Created
August 28, 2018 09:35
-
-
Save mohammad-fouladgar/ddd1564fb4e979f61c34a68a2746245c to your computer and use it in GitHub Desktop.
legacy code for making filter in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\User; | |
use Illuminate\Http\Request; | |
class UserController extends Controller | |
{ | |
public function index(Request $request) | |
{ | |
$users = User::where('is_active', true); | |
if ($request->has('age_more_than')) { | |
$users->where('age', '>', $request->age_more_than); | |
} | |
if ($request->has('gender')) { | |
$users->where('gender', $request->gender); | |
} | |
if ($request->has('has_published_post')) { | |
$users->where(function ($query) use ($request) { | |
$query->whereHas('posts', function ($query) use ($request) { | |
$query->where('is_published', $request->has_published_post); | |
}); | |
}); | |
} | |
return $users->get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment