简体   繁体   中英

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.

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM