Search through laravel relations with builder
up vote
1
down vote
favorite
I'm trying to work on a query builder in Laravel and want to search through a model's relations. So far my code looks like this:
$search = (new City)->newQuery();
// Search for a city based on its state.
if ($request->has('state')) {
$inquiry->whereHas('state', function ($query) use ($request) {
$query->whereState($request->state);
});
}
So I have a City
model and a State
model. The query is supposed to look through cities and then check each one's state relation and extract the model with the relevant states.
The code doesn't produce any errors, just an empty object.
php laravel eloquent
add a comment |
up vote
1
down vote
favorite
I'm trying to work on a query builder in Laravel and want to search through a model's relations. So far my code looks like this:
$search = (new City)->newQuery();
// Search for a city based on its state.
if ($request->has('state')) {
$inquiry->whereHas('state', function ($query) use ($request) {
$query->whereState($request->state);
});
}
So I have a City
model and a State
model. The query is supposed to look through cities and then check each one's state relation and extract the model with the relevant states.
The code doesn't produce any errors, just an empty object.
php laravel eloquent
Are you executing the query with->get()
?
– Jonas Staudenmeir
Nov 7 at 19:54
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to work on a query builder in Laravel and want to search through a model's relations. So far my code looks like this:
$search = (new City)->newQuery();
// Search for a city based on its state.
if ($request->has('state')) {
$inquiry->whereHas('state', function ($query) use ($request) {
$query->whereState($request->state);
});
}
So I have a City
model and a State
model. The query is supposed to look through cities and then check each one's state relation and extract the model with the relevant states.
The code doesn't produce any errors, just an empty object.
php laravel eloquent
I'm trying to work on a query builder in Laravel and want to search through a model's relations. So far my code looks like this:
$search = (new City)->newQuery();
// Search for a city based on its state.
if ($request->has('state')) {
$inquiry->whereHas('state', function ($query) use ($request) {
$query->whereState($request->state);
});
}
So I have a City
model and a State
model. The query is supposed to look through cities and then check each one's state relation and extract the model with the relevant states.
The code doesn't produce any errors, just an empty object.
php laravel eloquent
php laravel eloquent
edited Nov 7 at 12:49
executable
964221
964221
asked Nov 7 at 12:14
roo
9319
9319
Are you executing the query with->get()
?
– Jonas Staudenmeir
Nov 7 at 19:54
add a comment |
Are you executing the query with->get()
?
– Jonas Staudenmeir
Nov 7 at 19:54
Are you executing the query with
->get()
?– Jonas Staudenmeir
Nov 7 at 19:54
Are you executing the query with
->get()
?– Jonas Staudenmeir
Nov 7 at 19:54
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Try this:
City::when(request()->has('state'), function($query){
$query->whereHas('state', function ($query){
$query->where('state', request()->input('state'));
});
})->get()
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Try this:
City::when(request()->has('state'), function($query){
$query->whereHas('state', function ($query){
$query->where('state', request()->input('state'));
});
})->get()
add a comment |
up vote
0
down vote
Try this:
City::when(request()->has('state'), function($query){
$query->whereHas('state', function ($query){
$query->where('state', request()->input('state'));
});
})->get()
add a comment |
up vote
0
down vote
up vote
0
down vote
Try this:
City::when(request()->has('state'), function($query){
$query->whereHas('state', function ($query){
$query->where('state', request()->input('state'));
});
})->get()
Try this:
City::when(request()->has('state'), function($query){
$query->whereHas('state', function ($query){
$query->where('state', request()->input('state'));
});
})->get()
answered Nov 11 at 15:04
Amir Helmy
713
713
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189285%2fsearch-through-laravel-relations-with-builder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you executing the query with
->get()
?– Jonas Staudenmeir
Nov 7 at 19:54