lundi 13 février 2017

Nested hasMany eager loading Laravel function

Vote count: 0

Current logical setup:

  • A building can have many rooms.
  • A room can have many objects.
  • An object can have many numbers.

-- The problem is that the objects may/may not have any numbers.

Scenario: I want to retrieve only objects that have a number, based on the building_id I provide.

Attempted:

$audits = Building::where('id', $building_id)
            ->has('rooms.objects.scores')
            ->get();

What I'm finding is that it's returning all rooms and objects, even though only a couple of the objects actually have a score.

How I can query in reverse? Or the correct way! I only want objects that have a score associated with them, and consequently anything related to them (rooms etc..)

Many thanks.

asked 23 secs ago

Let's block ads! (Why?)



Nested hasMany eager loading Laravel function

Aucun commentaire:

Enregistrer un commentaire