【发布时间】:2023-03-21 13:52:01
【问题描述】:
我使用这个模型但是使用这个模型显示以下错误:
Failed calling App\User::jsonSerialize()
但删除 "$this->customer->name" 结果是可以的。 谢谢ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss。
class User extends Authenticatable
{
/**
* Get the user's customer name.
*
* @param string $value
* @return array
*/
public function getCustomerIdAttribute($value)
{
return [
'id' => $value,
'name' => $this->customer->name
];
}
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'customer_id' => 'array',
];
/**
* Get the customer record associated with the user.
*/
public function customer()
{
return $this->belongsTo(Customer::class);
}
}
【问题讨论】:
-
$user = Auth::user();返回 $user;
标签: laravel model eloquent foreign-keys