php - Laravel limit with (not globally) -
i have next query want execute eloquent
.
$restaurant = restaurant::with([ 'user' => function($query) { $query->select(['username']); // doesn't work $query->wherestatus('confirmed'); } ])->find($id);
i want fetch restaurant
, it's corresponding user
(they related) user
don't wan't information. username
in case.
i add together property $hidden
user
model, hidden every query (in array / json form). want limit bit more query.
if add together select (with or without wrapping in array) next result:
{ 'id': 1, 'user': null }
yet if remove select total user
:
{ 'id': 1, 'user': { .. json user info } }
the user
exist , username
not empty. how can go this?
here actual output can found.
edit: when alter value in select not exist in database see error, , deduct query correct, don't output. (query when using invalid column 'test').
select `test` `users` `users`.`deleted_at` null , `users`.`userable_id` in (1) , `users`.`userable_type` = restaurant , `status` = confirmed)
try way:
$restaurant = restaurant::with([ 'user' => function($query) { $query->wherestatus('confirmed')->lists('username'); } ])->find($id);
edit
it seems both methods work need fulfil 1 status - need include foreign_key column in lists/select. don't know relation illustration assuming have in users
table restaurant_id
column need utilize next code:
$restaurant = restaurant::with([ 'user' => function($query) { $query->select(['username', 'restaurant_id'])->wherestatus('confirmed'); } ])->find($id);
or
$restaurant = restaurant::with([ 'user' => function($query) { $query->wherestatus('confirmed')->lists('username','restaurant_id'); } ])->find($id);
i don't know if possible n:n relationship. should consider might bit unsafe (some problems may occur) should play if have strong reason grab selected columns.
php laravel eloquent
No comments:
Post a Comment