Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel whereHas

Laravel whereHas in eloquent Model Example

Aman Jain, October 2, 2022March 16, 2024

In this blog post, we’ll take a look at Laravel whereHas in eloquent Model Example and advantages. whereHas method is an important part of the Eloquent ORM. It allows you to add a constraint to a relationship query. whereHas method can be used to filter records based on a relationship. For example, you may want to find all customers who have purchased a particular product. To do this, you would use the whereHas method on the Customer model:

Customer::whereHas('orders', function ($query) use ($product) {
   $query->where('product_id', '=', $product->id);
})->get();


The whereHas method is also useful for identifying records that don’t have a particular relationship. For example, you may want to find all customers who have not placed an order in the last month. To do this, you would use the whereHas method with a negated closure:

Customer::whereHas('orders', function ($query) {
   $query->where('created_at', '<', Carbon::now()->subMonth());
}, '<', 1)->get();

The whereHas method can be a powerful tool in your Eloquent Model. Use it to filter records based on a relationship, or to identify records that don’t have a particular relationship.

When used correctly, the whereHas method can help you write more efficient Laravel applications.

Tips to use laravel whereHas in eloquent Model Example

There are a few things to keep in mind when using the whereHas method. First, the closure passed to the whereHas method is only executed when the whereHas method is called. Second, the whereHas method is executed before the main query, so you’ll need to take care when using eager loading.In MySQL we can do this by using inner join and same we can do in laravel using the join.

In this article we will take simple example of articles and users. We will filter the Users who has posted one article at least since register the account. To implement this example we will use hasMany and manyToOne relationship and that will filter the users data based on articles.

To accomplish this we will use has, whereHas and innerJoin in laravel

Here is the simple syntax

Model::has("RelativeModel",">",0)->get();

//OR

Model::whereHas(["RelativeModel"=>function($query){
  return $query->where("status",'1');
}])->get();

//OR

Model::join("realtive_table_name","realtive_table_name.model_primary_id","Model.id")
      ->where("status","1")->with("RelativeModel")->select("model.*")->get();

In the above statements we have used has , whereHas and innerJoin to filter the result from child model.

In first statement we count the number of relative model and getting the result on that condition. In second statement we used whereHas to filter data based on child model and in third we simply used join to filter data.

Let’s understand Laravel whereHas in eloquent Model Example

Step 1: Create a fresh laravel project

Open a terminal window and type below command to create a new project

composer create-project laravel/laravel blog

You can also read this to start with new project

Step 2 : Create a table and models

I assume the you have already installed the laravel and basic connection of it like database connection and composer.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel-jwt
DB_USERNAME=root
DB_PASSWORD=

Now, for an example i am creating here two table users and articles using migration and then i am going to fill the data in it, so create model and migration

php artisan make:model Article -m

this will generate the model and migration file

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
   
   use HasFactory;
    
}

and the default user model

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function articles(){
      $this->hasMany(Article::class);
    }
}

Here we create the relation between user and articles using hasMany relationship.

and migration file database/migrations/timestamp_create_articles_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return class extends Migration // for laravel 9
// class CreateArticle extends Migration // for laravel below 9
{
    /**
     * Run the migrations.
     *
     * @return void
     */
     public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('email')->unique();
            $table->int('user_id');
            $table->string('title');
            $table->string('body')->nullable();
            $table->enum('status',['1','0']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

and then migrate the migration

php artisan migrate

Step 3 : Fetch Relational Record

Now, we have create the models and relations, now fetch the filtered records by creating the routes and fetch data using three methods

Get Relational data using has method

In this method we will filter data using has method which count the records in child table and return the filter parent model

<?php

use Illuminate\Support\Facades\Route;
use App\Models\User;

Route::get('user-with-articles',function(){
 return  User::has("articles",">",0)->get();
});

This will return users who has posted at least 1 article.

Get Relational data using whereHas method

In this method we will filter data using whereHas method by which we can filter data by column name and where condition

<?php

use Illuminate\Support\Facades\Route;
use App\Models\User;

Route::get('user-with-articles',function(){
 return  User::whereHas("articles",function($query){
            $query->where("status",'1');
         })->get();
});

This will return users who has posted at least 1 article.

Get Relational data using Inner Join method

In this method we will filter data using inner join method by which we can filter data by column name and where condition as well

<?php

use Illuminate\Support\Facades\Route;
use App\Models\User;

 
Route::get('user-with-articles',function(){
 return  User::join("articles","articles.user_id", "users.id")->where("status","1")->count();
});

This will return users who has posted at least 1 article.

Related

Php Laravel Laravel 9 haslaravelwhereHas

Post navigation

Previous post
Next post

Related Posts

Php How to Get Headers Data from Request in Laravel

How to Get Headers Data from Request in Laravel ?

June 9, 2022November 10, 2023

In this article we will learn to get headers data from request in laravel. Http Request headers are used to send the information to server like what type of content clients accept, Content type or authorizations technique used to validate the request.To retrieve the headers data in laravel we need…

Read More
Php How to Add Google reCAPTCHA in Laravel 9 8 7 6 5

How to Add Google reCAPTCHA in Laravel 9 / 8 / 7 / 6 /5 ?

March 11, 2022March 11, 2022

Google reCAPTCHA used widely in may websites, in laravel its easy to use with third party package. Captcha is used to enhance the security of form. By adding the Captcha in laravel form we can prevent attackers to submit the form using the automated scripts and it adds an extra…

Read More
Php How to Create a Custom Artisan Command in LaravelHow to Create a Custom Artisan Command in Laravel

How to Create a Custom Artisan Command in Laravel ?

May 19, 2022May 26, 2022

Laravel artisan provides several inbuilt commands to help the developer to build a robust applications like to create the controller, models, migration etc. and in this article i will show you to create your own custom artisan command in laravel. This custom artisan can be configure according to your needs…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version