PHP code example of mountainclans / laravel-polymorphic-model

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

    

mountainclans / laravel-polymorphic-model example snippets


use MountainClans\LaravelPolymorphicModel\Traits\PolymorphicModel;

class YourParentModel {
    use PolymorphicModel;
    
    public const TYPE_TOP_BANNER = 'top_banner';
    public const TYPE_ADVANTAGES = 'advantages';
    
    public const ALLOWED_TYPES = [
        self::TYPE_TOP_BANNER => TopBannerSection::class,
        self::TYPE_ADVANTAGES => AdvantagesSection::class,
    ];
    
    protected $table = 'your_parent_model';
}

use MountainClans\LaravelPolymorphicModel\Traits\PolymorphicModel;

class TopBannerSection extends YourParentModel{
    protected function getInstanceType(): string
    {
        return static::TYPE_TOP_BANNER;
    }
}

$model = new TopBannerSection();
...
$model->save();

$collection = YourParentModel::withSubclasses()->get();

$collection = TopBannerSection::get();

$collection = YourParentModel::whereIn('type', [
    self::TYPE_TOP_BANNER, 
    self::TYPE_ADVANTAGES
])->get()

use MountainClans\LaravelPolymorphicModel\Traits\CheckOverrides;
use MountainClans\LaravelPolymorphicModel\Attributes\RequiresOverride;

class YourModel extends Model {
    use CheckOverrides;
    
    #[RequiresOverride]
    public function functionForOverride() {
        
    }
}