Answers for "when clause laravel"

PHP
1

laravel when

/* you may only want to apply a where statement if a given input value
is present on the incoming request.
*/
$role = $request->input('role');

$users = DB::table('users')
                ->when($role, function ($query, $role) {
                    return $query->where('role_id', $role);
                })
                ->get();
Posted by: Guest on March-21-2021
0

laravel when condition

$invisiblePosts = $request->query('invisible');

Article::query()
            ->when($invisiblePosts , function ($query){
                return $query->where('invisible' , true);
            },function ($query){
                return $query->where('invisible' , false);
            })
            ->get();
Posted by: Guest on February-12-2021

Browse Popular Code Answers by Language