Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boyjarv's avatar

Search not working?!

Only the first part of my search works?! propname Please help

$properties = Property::where('propname', 'LIKE', "%{$propname}%")
                ->where(static function ($query) use ($proptype_id, $propname, $minbeds, $category_id, $town) {
                    $query->where('proptype_id', '=', $proptype_id)
                        ->orWhere('propname', 'LIKE', "%".$propname."%")
                        ->orWhere('bedroom', '>', $minbeds)
                        ->orWhere('category_id', '=', $category_id)
                        ->orWhere('town', '=', $town);
                })->paginate(20);
0 likes
28 replies
bugsysha's avatar

Try

$properties = Property::where('propname', 'LIKE', "%{$propname}%")
                ->orWhere(static function ($query) use ($proptype_id, $propname, $minbeds, $category_id, $town) {
                    $query->where('proptype_id', '=', $proptype_id)
                        ->orWhere('propname', 'LIKE', "%".$propname."%")
                        ->orWhere('bedroom', '>', $minbeds)
                        ->orWhere('category_id', '=', $category_id)
                        ->orWhere('town', '=', $town);
                })->paginate(20);
boyjarv's avatar

now I get:

InvalidArgumentException
Illegal operator and value combination.
bugsysha's avatar

Your query does not make sense to me. Why are you wrapping/nesting? I have a feeling that you only need what is inside that closure/callback.

bugsysha's avatar
$properties = Property::where('proptype_id', '=', $proptype_id)
                        ->orWhere('propname', 'LIKE', "%".$propname."%")
                        ->orWhere('bedroom', '>', $minbeds)
                        ->orWhere('category_id', '=', $category_id)
                        ->orWhere('town', '=', $town)
                        ->paginate(20);
boyjarv's avatar

thanks but I am still getting:

InvalidArgumentException Illegal operator and value combination.

bugsysha's avatar
    protected function invalidOperatorAndValue($operator, $value)
    {
        return is_null($value) && in_array($operator, $this->operators) &&
             ! in_array($operator, ['=', '<>', '!=']);
    }

Check for these, if some of the variables is null or other stuff show in the method.

boyjarv's avatar

yes I think it's because I am using '=' and the other requested fields are null?!

boyjarv's avatar

so what can I use instead of '=' and multiple if statements?

bugsysha's avatar

Your variable should not be null and it should work. You can always remove = and use where('name', $value).

boyjarv's avatar

ok, I am now using the following:

$properties = Property::where('proptype_id', $proptype_id)
                ->orWhere('propname', 'LIKE', "%".$propname."%")
                ->orWhere('bedroom', $minbeds)
                ->orWhere('category_id', $category_id)
                ->orWhere('town', $town)
                ->paginate(20);

but only the propname search works?!

bugsysha's avatar
Property::where('proptype_id', $proptype_id)
                ->orWhere('propname', 'LIKE', "%".$propname."%")
                ->orWhere('bedroom', $minbeds)
                ->orWhere('category_id', $category_id)
                ->orWhere('town', $town)
		->toSql();

Copy that. Execute it on your database. See if it returns what you want. If not tweak it and post SQL here.

bugsysha's avatar

Assign it to a variable and dd that variable.

boyjarv's avatar
select * from `properties` where `proptype_id` is null or `propname` LIKE ? or `bedroom` is null or `category_id` is null or `town` is null

Error 1064 error in SQL syntax

bugsysha's avatar

That is probably why you are getting weird results. You should include those where statements only if the variable is not null.

boyjarv's avatar

So put an IF condition after each line?

bugsysha's avatar

I guess easies way for you would be something like

$query = Property::query();

if ($prop) {
	$query->where('name', $prop);
}

$query->paginate(20);

Repeat if statement for every prop and that is it.

achatzi's avatar

@bugsysha This is what I use for search

public function scopeSearch(Builder $builder, array $search_terms = []): Builder
{
    return $builder
    ->when(Arr::get($search_terms, 'email'), function ($query) use ($search_terms) {
        return $query->where('email', 'like', '%' . $search_terms['email'] . '%');
    })
    ->when(Arr::get($search_terms, 'name'), function ($query) use ($search_terms) {
        return $query->orWhere('name', 'like', '%' . $search_terms['name'] . '%');
    });
}

In your case replace Arr::get($search_terms, 'email') with the appropriate variable, ex $proptype_id

bugsysha's avatar

@achatzi

I use when() also, but I'm trying to ease him out of the confusion he put himself into.

boyjarv's avatar

now I'm getting this:

Facade\Ignition\Exceptions\ViewException
Call to undefined method Illuminate\Database\Eloquent\Builder::appends()
achatzi's avatar

@bugsysha I am sure you do. By the time I write my post, you had already replied.

I recently started using it myself, because I thought it was very ugly to have all these if's one after the other and it made it easier to read the code

boyjarv's avatar

Still not working?!

and here is my code so far:

if($propname||$minbeds||$proptype_id||$category_id||$town) {
            $properties = Property::where('proptype_id', $proptype_id)
                ->orWhereNotNull('propname', 'LIKE', "%".$propname."%")
                ->orWhereNotNull('bedroom', $minbeds)
                ->orWhereNotNull('category_id', $category_id)
                ->orWhereNotNull('town', $town)
                ->paginate(20);
}

PLEASE HELP?

achatzi's avatar

@boyjarv This is not what @bugsysha and I told you. You must add the orWhere if the variable is set.


$properties = Property::when($propname, function ($query) {
    return $query->orWhere('propname', 'LIKE', "%".$propname."%");
})
->when($proptype_id, function ($query) {
    return $query->orWhere('proptype_id', $proptype_id);
});

Continue like this with every variable;

bugsysha's avatar
bugsysha
Best Answer
Level 61

Yeah, you probably should look at some lessons here at Laracasts cause it looks like you are missing a bit of experience, and maybe that can get you up to speed.

Please or to participate in this conversation.