I'm using Laravel 8. I have 2 tables and trying to select specific columns from both tables using Eloquent:
$ids = [1, 8, 14];
$food = Food::with(
['animals' => function ($query)
{
$query->select('id', 'name');
$query->where('status', 1);
}])
->select('id', 'type')
->whereIn('id', $ids)
->where('status', 1)
->get();
However, if I add a select
method to the Food
model, Laravel returns null
for the animals
table.
If I don't specify selects on the Food
model, Laravel returns everything from the Food
model and id
, name
from the animals
table.
How can I select both from the Food
and the Animal
table simultaneously, while using Eager Loading?