In this tutorial, we will see how to use laravel wherehas eloquent. You will learn about laravel eloquent whereHas().

Given below is the example of laravel wherehas eloquent.




Example WhereHas Eloquent


<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Country;

class PostController extends Controller
{
   public function index()
   {
       $country = Country::whereHas('posts', function($q){
                      $q->where('name', '=', 'Good Info');
                   })->get();  
       dd($country->toArray());
   }
}

OUTPUT:

array:1 [
  0 => array:5 [
    "id" => 1
    "name" => "test"
    "posts_id" => 1
    "created_at" => null
    "updated_at" => null
  ]
]


I hope this example helps you.