PHP code example of pindab0ter / constrained-morph-to-for-laravel

1. Go to this page and download the library: Download pindab0ter/constrained-morph-to-for-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pindab0ter / constrained-morph-to-for-laravel example snippets


// Standard Laravel morphTo - returns ANY model type
public function commentable()
{
    return $this->morphTo();
}

$comment->commentable; // Returns any model type

use pindab0ter\ConstrainedMorphtoForLaravel\HasConstrainedMorphTo;

class Comment extends Model
{
    use HasConstrainedMorphTo;

    /** @return ConstrainedMorphTo<Post, $this> */
    public function post()
    {
        return $this->constrainedMorphTo(Post::class, 'commentable_type', 'commentable_id');
    }

    /** @return ConstrainedMorphTo<Post|Video, $this> */
    public function commentable()
    {
        return $this->constrainedMorphTo(
            [Post::class, Video::class], // Accept multiple types
            'commentable_type',
            'commentable_id'
        );
    }
}

$comment->post; // Returns a Post if the type matches, null if it doesn't
$comment->commentable; // Returns a Post or Video if the type matches, null otherwise

    use Illuminate\Database\Eloquent\Model;
    use pindab0ter\ConstrainedMorphtoForLaravel\HasConstrainedMorphTo;

    class Comment extends Model
    {
        use HasConstrainedMorphTo;

        /** @return ConstrainedMorphTo<Post, $this> */
        public function commentable()
        {
            return $this->constrainedMorphTo(
                Post::class,        // Only allow Post models
                'commentable_type', // The type column name
                'commentable_id'    // The ID column name
            );
        }
    }
    

    $post = Post::create([...]);
    $comment = Comment::create([
        'commentable_id' => $post->id,
        'commentable_type' => Post::class,
    ]);

    $comment->commentable; // Returns the Post instance
    

    $image = Image::create([...]);
    $comment = Comment::create([
        'commentable_id' => $image->id,
        'commentable_type' => Image::class, // Wrong type!
    ]);

    $comment->commentable; // Returns null (constraint not met)
    

/** @return ConstrainedMorphTo<Post|Video, $this> */
public function commentable()
{
    return $this->constrainedMorphTo(
        [Post::class, Video::class], // Accept both Post and Video models
        'commentable_type',
        'commentable_id'
    );
}

$post = Post::create([...]);
$comment1 = Comment::create([
    'commentable_id' => $post->id,
    'commentable_type' => Post::class,
]);
$comment1->commentable; // Returns the Post instance

$video = Video::create([...]);
$comment2 = Comment::create([
    'commentable_id' => $video->id,
    'commentable_type' => Video::class,
]);
$comment2->commentable; // Returns the Video instance

$image = Image::create([...]);
$comment3 = Comment::create([
    'commentable_id' => $image->id,
    'commentable_type' => Image::class, // Not in allowed types!
]);
$comment3->commentable; // Returns null

public function commentable()
{
    return $this->constrainedMorphTo(
        Post::class,        // Constrained type (or array of types)
        'commentable_type', // Type column
        'commentable_id',   // ID column
        'commentable',      // Relationship name (optional)
        'id'                // Owner key (optional)
    );
}