简体   繁体   中英

Laravel - hasManyThrough return only _id

I use Laravel MongoDB package by jenssegers and Eloquent Laravel Model.

articles :

  • _id (ObjectID)
  • feed_id
  • title

feeds :

  • id
  • user_id
  • name

users :

  • id
  • name

hasManyThrough in User::class model to get all articles by one user.

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

I get only _id (ObjectID) with this query:

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

Could you help me to search the problem?

You can try this

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

In place of find() you can use first() if you want the first record and if you want all records you can use get()

also if you want to put constraint use where() rather than find()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM