jeudi 18 juillet 2019

Saving customer model inherited from User can not read id

In my Laravel 5 table users can be of different type, so I made customer model, like

class Customer extends User 
{
    protected $table = 'users';
    protected $primaryKey = 'users.id';

    protected $fillable = [
       ...
    ];


    public static function boot()
    {
        parent::boot();
        static::addGlobalScope('RoleCustomer', function (Builder $builder) {
            $builder->leftJoin( 'users_groups', 'users_groups.user_id', '=', 'users.id')
            ->select( with(new Customer)->getTable().'.*')
                ->where( 'users_groups.group_id', ACCESS_ROLE_CUSTOMER );

        });
    }

and it works when I need to load data but I encountered error saving data : But I have problems saving :

    $newCustomer = new Customer();
    ...
    $newCustomer->save();

    $newUsersGroups= new UsersGroups();
    $newUsersGroups->user_id= $newCustomer->id; // this value is mull

and dumping newCustomer var I see :

[attributes:protected] => Array
    (
        [username] => customer1
        ...
        [users.id] => 13
    )

I am not sure which is valid way to deal it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire