属性 [所有者] 在此 collection 实例上不存在

Property [owner] does not exist on this collection instance

我有点难以理解 Laravel 试图在这里传达的内容以及 Eloquent 如何 building/attaching 这些关系。

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.

我已经通过 owner

的 public 方法在 $this->belongsTo(User::class,'user_id'); 的 Ticket 模型中定义了关系
public function owner()
    {
        // ESTABLISH RELATIONSHIP

        $this->belongsTo(User::class, 'user_id');

    }

但是当我尝试检索下面的内容时,我检索到标题中的错误。来自 TicketsController

$tickets = Ticket::latest()->get();
return $tickets->owner;

我无法 {{ $ticket->owner->first_name }} 或我想提取的有关用户的任何详细信息。

是我对 Eloquent 的基本理解不正确,还是需要分配其他内容才能启用此功能?

我一直在关注 Laracast 的这一集,在这里用 Thread 代替了 Ticket,但 Reply 命名空间保留了下来。尽管我没有检索与回复关联的用户数据,而是检索了初始票证的用户数据。

https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/3

$tickets = Ticket::latest()->get();
return $tickets->owner;

此代码获取集合 票。 该集合中的每张 门票都有其 自己的 所有者。

foreach($tickets as $ticket) {
    // this is where you have $ticket->owner;
}

您的 owner() 函数也不会 返回 关系 - 它创建一个关系,但不对其进行任何操作。这会起作用:

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