Strict Enforcement
Type-safe polymorphic relationships with strict mode.
Enabling Strict Mode
Section titled “Enabling Strict Mode”return [ 'strict' => true,
'morphs' => [ 'commentable' => [ 'post' => App\Models\Post::class, 'video' => App\Models\Video::class, ], ],];What Strict Mode Does
Section titled “What Strict Mode Does”Prevents Invalid Types
Section titled “Prevents Invalid Types”// With strict mode ON$comment = new Comment();$comment->commentable()->associate($product); // Throws exception!// "Product is not a valid commentable type"
// Only configured types allowed$comment->commentable()->associate($post); // Works$comment->commentable()->associate($video); // WorksValidates on Save
Section titled “Validates on Save”// Even manual assignment is validated$comment = new Comment();$comment->commentable_type = 'product'; // Invalid$comment->commentable_id = 1;$comment->save(); // Throws MorphismException!Validates on Query
Section titled “Validates on Query”// Invalid morph type in queryComment::where('commentable_type', 'invalid')->get();// Throws exception in strict modeStrict Mode Levels
Section titled “Strict Mode Levels”return [ // Full strict - validates everything 'strict' => true,
// Or granular control 'strict' => [ 'save' => true, // Validate on model save 'query' => true, // Validate in queries 'associate' => true, // Validate on associate() 'create' => true, // Validate on create/make ],];Exception Handling
Section titled “Exception Handling”use Cline\Morphism\Exceptions\InvalidMorphTypeException;
try { $comment->commentable()->associate($invalidModel);} catch (InvalidMorphTypeException $e) { // Handle invalid type $validTypes = $e->getValidTypes(); $attemptedType = $e->getAttemptedType(); $morphName = $e->getMorphName();}Disabling Per-Operation
Section titled “Disabling Per-Operation”use Cline\Morphism\Facades\Morphism;
// Temporarily disable strict modeMorphism::withoutStrict(function () { $comment->commentable()->associate($anyModel);});
// Or for specific morph$comment->commentable()->withoutStrict()->associate($anyModel);Development vs Production
Section titled “Development vs Production”return [ // Strict in development, permissive in production 'strict' => env('APP_ENV') !== 'production',
// Or always strict but log instead of throw in production 'strict' => true, 'strict_action' => env('APP_ENV') === 'production' ? 'log' : 'throw',];Custom Validation
Section titled “Custom Validation”use Cline\Morphism\Facades\Morphism;
Morphism::validateUsing('commentable', function ($model, $type) { // Custom validation logic if ($model instanceof Post && !$model->allow_comments) { return false; } return true;});