I have the following query I want to execute with Eloquent
.
$restaurant = Restaurant::with([
'user' => function($query)
{
$query->select(['username']); // This doesn't work
$query->whereStatus('confirmed');
}
])->find($id);
I want to fetch a Restaurant
and it's corresponding User
(they are related) but from the User
I don't wan't all the information. Only the username
in this case.
I could add a property $hidden
to the User
model, but then they would be hidden for every query (in array / json form). But I only want to limit it a bit more for this query.
If I add the select (with or without wrapping it in an array) I get the following result:
{
'id': 1,
'user': null
}
Yet if I remove that select I get the full User
:
{
'id': 1,
'user': { .. json user data }
}
The User
does exist and the username
is not empty. How can I go about this?
Here the actual output can be found.
Edit: When I change the value in the select to something that does not exist in the database I see an error, and from that I deduct that the Query is correct, but I just don't get any output. (Query I get when using an invalid column 'test').
select `test` from `users` where `users`.`deleted_at` is null and `users`.`userable_id` in (1) and `users`.`userable_type` = Restaurant and `status` = confirmed)