I have the following three tables:
A
-------------
| id | name |
-------------
B
--------------------
| id | A_id | name |
--------------------
C
--------------------
| id | B_id | name |
--------------------
So, the data in table C
belongs to the data in table B
which belongs to the data in table A
. Now, I want to query C
, while also retrieving data from B
and A
and the following code does the trick just fine.
C::with('B.A')->get();
The problem now, is that I want to query C
with some constraints. One of these constraints is the id
of A
. I've tried the following:
C::with(array('B.A' => function ($query)
{
$query->where('id', '=', $constraint);
}))->get();
But it seems that Eloquent will retrieve all the rows in C
without even taking the constraint into account, except when it's executing the query to retrieve data in table A
. How do I get around this problem? Do I need to add another field in C
, that is A_id
, and match $constraint
against that field?