Skip to content

michielkempen/laravel-morphable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Morphable

A trait to morph Eloquent models to a child class based on a type field.

Installation

Add the package to the dependencies of your application

composer require michielkempen/laravel-morphable

Usage

Add the Morphable trait to your model, and add a factory and typeField property. The typeField property is optional and defaults to type. The factory property is mandatory and needs to contain the path to a class which inherits the MorphableModelFactory interface.

<?php

use Illuminate\Database\Eloquent\Model;
use MichielKempen\LaravelMorphable\Morphable;
use App\UserModelFactory;

class User extends Model
{
    use Morphable;
    
    public $factory = UserModelFactory::class;
    
    public $typeField = 'role';
}
<?php

use Illuminate\Database\Eloquent\Model;
use \MichielKempen\LaravelMorphable\MorphableModelFactory;

class UserModelFactory implements MorphableModelFactory
{
    public static function create(string $type): Model
    {
        if($type == 'admin') {
            return app(Manager::class);
        }   
    
        return app(Customer::class);
    }
}
<?php

class Manager extends User
{
    // manager specific methods
}
<?php

class Customer extends User
{
    // customer specific methods
}

Security

If you discover any security related issues, please email kempenmichiel@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages