Skip to content

hasMany relationts not working on subdocuments #1817

Closed
@luismabenitez

Description

@luismabenitez

I'm not sure this part is working properly.

Just trying to do some relationship but I'm getting a null on my requests.

I have a "passengers" collection with a trip_id. Something like this:

{  
   "_id":"5d680fd36882d19e782d67bd",
   "trip_id":"5d680fcb6882d19e782d6380",
   "name":"Lee Jones"
}

And a collection with the name "lotes" that contain documents that each one has many trips. Something like this:

{  
   "_id":"5d680fca6882d19e782d60f6",
   "user_id":"5d680fca6882d19e782d60b4",
   "date_to_ride":"2019-08-29T22:00:00.000+00:00",
   "trips":[  
      {  
         "trip_id":"5d680fcb6882d19e782d6380",
         "car":"VW Golf"
      }
   ]
}

So I want to get all passengers that has same trip_id when I'm getting the trip info.

This is my relation on the Trip model:

public function passengers()
{
    return $this->hasMany(\App\Passenger::class, 'trip_id', 'trips.trip_id')->whereNotNull('trip_id');
}

My result? "passengers": []

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions