【问题标题】:Laravel - hasManyThrough return only _idLaravel - hasManyThrough 仅返回 _id
【发布时间】:2017-09-22 19:20:23
【问题描述】:

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

文章:

  • _id (ObjectID)
  • feed_id
  • 标题

供稿:

  • 身份证
  • user_id
  • 姓名

用户:

  • 身份证
  • 姓名

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);

你能帮我搜索一下问题吗?

【问题讨论】:

    标签: php mongodb laravel eloquent laravel-5.5


    【解决方案1】:

    你可以试试这个

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

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

    如果您想设置约束,请使用where() 而不是find()

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

    【讨论】:

    • 您能在此处发布您从该查询中获得的 sql 吗?
    猜你喜欢
    • 2020-06-26
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 2018-10-04
    • 2014-09-19
    相关资源
    最近更新 更多