pirmax
pirmax

Reputation: 2153

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?

Upvotes: 1

Views: 439

Answers (1)

Mr. Pyramid
Mr. Pyramid

Reputation: 3935

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

Upvotes: 0

Related Questions