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

0

Conditional Clause in query, run query if something true with multiple clause

$sortByVotes = $request->input('sort_by_votes');

$users = DB::table('users')
                ->when($sortByVotes, function ($query, $sortByVotes) {
                    return $query->orderBy('votes');
                }, function ($query) {
                    return $query->orderBy('name');
                })
                ->get();
Posted by: Guest on April-27-2021

Code answers related to "Conditional Clause in query, run query if something true with multiple clause"

Browse Popular Code Answers by Language