在我的Laravel 5表中,用户可以是不同类型,因此我建立了客户模型,例如
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 );
});
}
,它在需要加载数据但遇到保存数据错误时起作用: 但是我在保存时遇到了问题:
$newCustomer = new Customer();
...
$newCustomer->save();
$newUsersGroups= new UsersGroups();
$newUsersGroups->user_id= $newCustomer->id; // this value is mull
并转储newCustomer var我看到了:
[attributes:protected] => Array
(
[username] => customer1
...
[users.id] => 13
)
我不确定哪种有效的处理方式?