user2636197 user2636197 - 1 month ago 18
MySQL Question

Laravel optional where() query

I wonder if there is something like a optional where query. Eg:

Select * from table where X must be = 2 and Y must be equal to 0 or 1?

Answer

You may try as:

$userId = $request->input('userId');
->where('X', 2)
->where(function($q) use($userId) {
    $q->where('Y', $userId)
        ->orWhere('Y', 1)
})
Comments