Laravel Eloquent choosing which model to find

I have a Player model that has homeAddress which has homeAddressPhoto, linked by foreign key.

I could get the list of home address photos by calling this:

Player::find($player_id)->homeAddress->homeAddressPhoto;

Now I want to find the photos by homeAddressPhoto id, but the code below finds the photo by homeAddress id.

Player::find($player_id)->homeAddress->homeAddressPhoto()->where('id',$photo_id)->first();

How should I write the where part to get the id from homeAddressPhoto instead of homeAddress?

1 answer

  • answered 2018-07-11 06:01 Jinal Somaiya

    try this:

    Player::find($player_id)->homeAddress->homeAddressPhoto->where('id',$photo_id)->first();