laravel eager loading where clause 1

laravel eager loading where clause

$query = ""    

if ($request->has('country'){
$query = Post::with("country")->whereHas("country",function($q) use($request){
    $query = Post::with("country")->get();

Here is what the above code is Doing:
1. We are checking if the request has a country parameter.
2. If it does, we are using the whereHas method to filter the posts that have a country with the name of the country that was passed in the request.
3. If it doesn’t, we are just getting all the posts with their countries.

Now, let’s see how we can use this in our controller:

Similar Posts