相關表列(customers.name
)在所有選擇的模型(用戶)laravel。 我使用訪問器laravel。顯示所有選擇模型laravel
用戶表:
id name customer_id
1 hassan 1
客戶表:
id name
1 customer1
現在
使用
$user = Auth::user();
return $user;
我想顯示:
id: 1,
name: "hassan",
customer_id: {
id: 1,
name: "customer1"
}
但顯示這個錯誤:
失敗調用應用程序\用戶:: jsonSerialize()
class User extends Authenticatable
{
use EntrustUserTrait;
/**
* 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);
}
}
解釋清楚,你想得到什麼? –
編輯答案,請再次顯示。謝謝。 – HassanDL
如果你有很好的解決方案(accessor laravel除外), 請說明。 – HassanDL