Laravel 8 Eloquent whereHas Condition

Websolutionstuff | Oct-06-2021 | Categories : Laravel

In this example we will see laravel 8 eloquent whereHas() condition. you will learn about wherehas() condition in laravel 8. I will give you simple example of how to use wherehas() condition with laravel eloquent relationship. you can also use with laravel 6, laravel 7 and laravel 8 version.

For wherehas condition in laravel 8 we need two table like users and country and in users tabel add country_id.

So, let's start to implements laravel 8 whereHas() realationship.

Example 1 : Laravel whereHas()

Laravel eloquent whereHas() method works basically the same as has() but it just allows you to specify additional filters for the related model to check. 

public function index()
{
    $name = 'india';
    $users = User::with('country')
                    ->whereHas('country', function ($query) use($name){
                        $query->where('name', 'like', '%'.$name.'%');
                    })
                    ->get()
                    ->toArray();
}

 

 

Example 2 : Laravel has()

 Laravel eloquent has() method is used to filter the selecting model based on the selected relationship. It works similarly to where method for relations.

use App\Models\User;

$users = User::has('country')->get();

You may also specify an operator and count value to further customize the query: 

$posts = Post::has('comments', '>=', 3)->get();

 

Example : 3

app/Models/User.php

<?php
  
namespace App\Models;
  
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
  
class User extends Authenticatable
{
    
    protected $fillable = [
        'name',
        'email',
        'password',
    ]; 
  
    public function country()
    {
        return $this->belongsTo(Country::class);
    }
}

app/Models/Country.php

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class Country extends Model
{
    use HasFactory;
}

 

app/Http/Controllers/UserController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{
    public function index()
    {
        $name = 'india';
        $users = User::with('country')
                        ->whereHas('country', function ($query) use($name){
                            $query->where('name', 'like', '%'.$name.'%');
                        })
                        ->get()
                        ->toArray();  
    }
}

 


You might also like :

Recommended Post
Featured Post
How To Create Candlestick Chart In Laravel 9 Using Highcharts
How To Create Candlestick Char...

In this article, we will see how to create a candlestick chart in laravel 9 using highcharts. A candlestick is a ty...

Read More

Oct-06-2022

Laravel 9 Has Many Through Relationship Example
Laravel 9 Has Many Through Rel...

In this article, we will see that laravel 9 has many through relationship example. hasManyThrough relationship is diffic...

Read More

Apr-04-2022

How To Create Table Using Migration In Laravel 10
How To Create Table Using Migr...

In this article, we will see how to create a table using migration in laravel 10. Migrations are like version contr...

Read More

Apr-21-2023

How to Get Request Header in Laravel 10
How to Get Request Header in L...

Ever wondered how to access request headers in your Laravel 10 application? Join me as I guide you through a quick and s...

Read More

Dec-11-2023