簡體   English   中英

Laravel:為什么不工作belongsTo()?

[英]Laravel: Why not work belongsTo()?

我必須在視圖中打印所有帶有發件人姓名的消息。 我有帶有以下代碼的Inbox模型:

protected $fillable = ['id', 'subject', 'message', 'sender', 'recipient', 'seen', 'show', 'trashed', 'created_at'];

public function sender() {
    return $this->belongsTo(User::class, 'sender');
}

public function recipient() {
    return $this->belongsTo(User::class, 'recipient');
}

我在哪里返回並打印給定錯誤的$message->sender->name Trying to get property of non-object$message->sender = sender id 使用發件人ID,我必須從用戶表中獲取發件人名稱。

您需要為這些關系選擇其他名稱,因為您已經擁有具有相同名稱的屬性。

public function messageSender()
{
    return $this->belongsTo(User::class, 'sender');
}

public function messageRecipient()
{
    return $this->belongsTo(User::class, 'recipient');
}

或更改列名sender_idrecipient_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM