Laravel - hasManyThrough return 只有 _id

Laravel - hasManyThrough return only _id

我使用 jenssegers 的 Laravel MongoDB 包和 Eloquent Laravel 模型。

articles :

  • _id (ObjectID)
  • feed_id
  • title

feeds :

  • id
  • user_id
  • name

users :

  • id
  • name

hasManyThrough 在 User::class 模型中获取一个用户的所有文章。

public function articles()
{
    return $this->hasManyThrough(
        'App\Article',
        'App\Feed',
        'user_id', // Foreign key on feeds table...
        'feed_id', // Foreign key on articles table...
        '_id', // Local key on users table...
        'id' // Local key on feeds table...
    );
}

我通过此查询仅获得 _id (ObjectID):

$user = \App\Models\User::find(1);
dd($user->articles);

你能帮我搜索问题吗?

你可以试试这个

$user->articles()->find(1);

如果你想要第一条记录,你可以使用 first() 代替 find(),如果你想要所有记录,你可以使用 get()

如果你想使用 where() 而不是 find()

$user->articles()->where('_id', 1)->first();