Ray Smith
Ray Smith

Reputation: 55

Multiple where inside a where statement

I would like to generate a query in Laravel that gives the following results:

From the results obtained from

$results = DB::table('records')->where('email', '!=', '[email protected]')->get();

Do multiple where statements like this:

->where('id', 'like', '%'.$request->search.'%')
->orWhere('email', 'like', '%'.$request->search.'%')
->orWhere('recordType', 'like', '%'.$request->search.'%')
->orWhere('uploadDate', 'like', '%'.$request->search.'%')
->orWhere('uploadTime', 'like', '%'.$request->search.'%')
->get();

How should I phrase the statement in controller to produce the desired result?

Upvotes: 2

Views: 76

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

Use the where() closure to group parameters:

$results = DB::table('records')->where('email', '!=', '[email protected]')
    ->where(function($q) use($request) {
        $q->where('id', 'like', '%' . $request->search . '%')
          ->orWhere('email', 'like', '%' . $request->search . '%')
          ->orWhere('recordType', 'like', '%' . $request->search . '%')
          ->orWhere('uploadDate', 'like', '%' . $request->search . '%')
          ->orWhere('uploadTime', 'like', '%' . $request->search . '%');
    })
    ->get();

Upvotes: 2

Related Questions