Please show full. It looks like you are getting every single row from your database instead of letting the database handle the queries
Skip filter if there is no result against query.
Hi there. I'm stuck with a little problem need help. I'm filtering records of a model and its relationships. The problem is If the statement becomes false the $matches variable becomes an empty array. How can I use the IF statement so the $matches variable doesn't become an empty array so it can go to the next condition? this is a sample code I can show full if anyone need.
$matches = Model::with('someRelation'); //can't use get() because want to filter data on relation.
if ($matches->get()->where('column', value)->count() > 0)
{
$matches->where('column', value);
}
if ($matches->whereRelation('someRelation', 'column', value)->count() > 0)
{
$matches->whereRelation('someRelation', 'column', value);
}
$matches become empty if the statement is false. whereRelation is a function introduced in laravel 8 replacement of whereHas. The First if working fine because i'm using get() and the chaining where how can I do the same thing with relation so the variable don't become empty.
I think you are going to have to write an algorithm for this. As I ran the Zillow example in my neighbor the most obvious place to purposely get no results was the price. I'm sure there are times when someone wants an 8 bedroom house. But I think people mostly tend to want more than they can pay for.
So maybe the real question is which area do you show them instead of the one they searched?
Because let's say, you run your search and remove the price filter. then the user is going to be like, "wtf I can't afford these?"
So two thoughts,
-
Do a query for the count for each filter first. I bet Zillow caches this daily or something. Then if it fails do the other query... whatever you decided.
-
Just run the query and if it fails, assume it failed in an order of priority ->
price, beds, sqftand remove one and run it again with, whatever else you decided. But again what are you going to show them?
As a user, I'd rather get "no results match your filters" Please don't tell me what you think I want to see.
Please or to participate in this conversation.