简体   繁体   中英

How to select all in laravel eloquent

Is it possible to query all in case the information of the following variable is not pass to the function.

$user = User::where('isVerified', $a)
        ->where('country', $b)
        ->where('province', $c)
        ->orderBy('created_at','desc')->paginate(6);

How can i make this query available for the data getting in case without information of country, province pass to the query? Which mean any data is verified. at the same time, i need to get the filtered data in case they do provide. Thanks.

$user = User::where('isVerified', $a);

if ($b)
{
    $user->where('country', $b)
}

if ($c)
{
    $user->where('country', $c)
}

$user->orderBy('created_at','desc')->paginate(6);

You might need to use isset($b) if you don't set them to null if nothing is set.

Build your query dynamically and use isset() to check if the variable values are set or not.

$user = User::where('isVerified', $a);

if(isset($b)){
   $user -> where('country', $b);
}

if(isset($c)){
   $user -> where('country', $c);
}

$user -> orderBy('created_at', 'desc') -> paginate(6);

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