Laravel WhereHas Eloquent Example Tutorial

Laravel 6 , Laravel , Laravel 7

Nicesnippets

1101

09-07-2020


Laravel WhereHas Eloquent Example Tutorial

Hi Guys,

In this tutorial,I will explain you how to laravel wherehas eloquent. you will learn about laravel eloquent whereHas() with example. we show example of wherehas eloquent in laravel.

you can easily use laravel wherehas eloquent.

Here I will give you example of laravel wherehas eloquent.

Example WhereHas Eloquent

Now this example, If you need even more power, you may use the whereHas methods to put "where" conditions on your has queries.

<?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

]

]

It will help you..


Recommended Posts