我有点难以准确地理解拉雷维尔在这里试图传达的内容,以及建立/连接这些关系的雄辩程度。
Property [owner] does not exist on this collection instance.
我正在创建一个客户支持收件箱,客户可以在其中发送票据,管理员可以回复。
我希望能够提取名称&消息中包含用户的一些详细信息,这样,代表就知道他们在跟谁说话。目前有三张桌子在使用中:
tickets, users, & replies
Tickets: id, user_id, body, active (boolean), datestuff... | relates to the Ticket model
replies: id, user_id, ticket_id, body, datestuff | relates to the Reply model.
我已经在的票证模型中定义了关系
$this->belongsTo(User::class,'user_id');
通过公共方法
owner
public function owner()
{
// ESTABLISH RELATIONSHIP
$this->belongsTo(User::class, 'user_id');
}
但当我试图检索以下内容时,我检索到了标题中的错误。来自TicketController
$tickets = Ticket::latest()->get();
return $tickets->owner;
{{ $ticket->owner->first_name }}
或者我想了解的关于用户的任何详细信息。
要么是我对Eloquent的基本理解不正确,要么是为了启用此功能,需要分配其他内容?
我一直在关注Laracast的这一集,在这里用线程代替票证,但回复命名空间保留了下来。虽然我没有检索与回复相关联的用户数据,而是检索初始票据的用户数据。
https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/3