when query with if condition laravel code example
Example 1: 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();
Example 2: laravel if else condition in query
$query = SocialMediaFeed::where('location_id', $location_id);
if(!$filters) {
$query = $query->where('self', '<>', true);
} else {
$query = $query->orWhere('self', true);
}