繁体   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