PHP code example of moneo / laravel-morphmap

1. Go to this page and download the library: Download moneo/laravel-morphmap 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/ */

    

moneo / laravel-morphmap example snippets


class Post extends Model
{
    // 🚀 Add HasCustomMorphMap trait!
    use HasCustomMorphMap;

    public function __construct(array $attributes = [])
    {
        // 👋 Custom definition for Category relation!    
        $this->customMorphMap = [
            Category::class => 'post',
        ];
        
        // 👋 Default for all others! (__CLASS__ definition is default. You don't need to add this.)
        $this->defaultMorphType = Post::class;

        parent::__construct($attributes);
    }

    public function tags(): MorphToMany
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }

    public function categories(): MorphToMany
    {
        return $this->morphToMany(Category::class, 'categoryable');
    }
}