Rubberduck1337106092
Rubberduck1337106092

Reputation: 1344

How to make a multiple where clause with Laravel Elequent?

$data = Retour::where('status','=',3)->where('rma','LIKE', '%' .$searchquery.'%')->OrWhere('naam','LIKE', '%' .$searchquery.'%')->OrWhere('framenummer','LIKE', '%' .$searchquery.'%')->get();

What's wrong with this query?

It ignores the where status = 3..
and returns all the reconrds even where the status != 3..

Thanks in advance.

Upvotes: 1

Views: 58

Answers (2)

aimme
aimme

Reputation: 6818

Use scopes in such cases. See https://laravel.com/docs/5.3/eloquent#local-scopes

public function scopeSearch($query, $searchQuery)
{
        return $query->where('status',3)
                     ->where('rma','LIKE', "%$searchQuery%")
                     ->orWhere('naam','LIKE', "%$searchQuery%")
                     ->orWhere('framenummer','LIKE', "%$searchQuery%");
}

and one more thing

its not OrWhere it is orWhere

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

You should group orWhere() here with closure:

$data = Retour::where('status', 3)
    ->where(function($q) use($searchquery) {
        $q->where('rma', 'like', '%'.$searchquery.'%')
          ->orWhere('naam', 'like', '%'.$searchquery.'%')
          ->orWhere('framenummer', 'like', '%'.$searchquery.'%');
    })->get();

Upvotes: 1

Related Questions