PHP code example of biscofil / laravel-submodels

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

    

biscofil / laravel-submodels example snippets

 php
class User extends Authenticatable{

    use HasSubModels;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name', 'is_admin'
    ];

    /**
    * @param $model
    * @return string|null
    */
   public function getSubModelClass($model){
       $class = null;
       if ($model->isAdmin()) {
           $class = AdminUser::class;
       } elseif ($model->isCustomer()) {
           $class = CustomerUser::class;
       }
       return $class;
   }

   /**
     * @param $query
     * @return mixed
     */
    public function scopeAdmins($query)
    {
        return $query->where('is_admin', '=', true);
    }

}
 php
class AdminUser extends User{

    use HasAppendedFields;

    private $appendedFillable = [
        'admin_parameter',
        'is_a_cool_admin'
    ];

    private $appendedCasts = [
         'is_a_cool_admin' => 'bool'
    ];

    public function newQuery()
    {
        return $this->scopeAdmins(parent::newQuery());
    }

}