Answers for "Conditional Clause in query, run query if something true"

PHP
0

Conditional Clause in query, run query if something true

$role = $request->input('role');

$users = DB::table('users')
                ->when($role, function ($query, $role) {
                    return $query->where('role_id', $role);
                })
                ->get();
Posted by: Guest on September-14-2020

Browse Popular Code Answers by Language