Laravel 6 Eloquent Has With and WhereHas

Today, We want to share with you Laravel 6 Eloquent Has With and WhereHas.In this post we will show you laravel nested relationships, hear for laravel wherehas with parameter we will give you demo and example for implement.In this post, we will learn about Using Eloquent Has, With, WhereHas with an example.

In Laravel’s Eloquent ORM, the whereHas() method allows you to query based on the existence of related models. It’s particularly useful when you have a relationship defined between two models and you want to filter the results based on conditions on the related model.

Here’s how you can use whereHas():

Let’s say you have two models: User and Post, and there’s a one-to-many relationship between them (i.e., a user has many posts).

use App\Models\User;
use App\Models\Post;

// Retrieve users who have at least one post with a certain condition
$usersWithPosts = User::whereHas('posts', function ($query) {
    $query->where('status', 'published');
})->get();

In this example:

  • We’re querying the User model.
  • We use the whereHas() method to specify a condition on the related Post model.
  • The closure passed to whereHas() defines the condition on the related Post model. In this case, we’re filtering posts with a status of 'published'.
  • The get() method retrieves the users who meet the specified condition.

You can use whereHas() with any type of Eloquent relationship: hasOne, hasMany, belongsTo, belongsToMany, etc. It’s a powerful tool for filtering query results based on conditions on related models.

Laravel 6 Eloquent Has With and WhereHas

There are the Following The simple About Using WhereHas in Laravel Polymorphic Relations Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to use with in laravel, so the Laravel – Eloquent “Has”, “With”, “WhereHas” is used for this example is following below.

Using Laravel 6 Eloquent Has, With, WhereHas

using With

Visitor > hasMany > Product
$visitors = Visitor::with('products')->get();

foreach($visitors as $visitor){

    $visitors->products; 

}

using Has Example

Visitor > hasMany > Product
$visitors = Visitor::has('products')->get();

using WhereHas Example

$visitors = Visitor::whereHas('products', function($q){
    $q->where('created_at', '>=', '2020-01-01 00:00:00');

})->get();

How to use whereHas with orWhereHas in Laravel 6?

public function allVisitors()
{
    $visitors = Visitor::with(['product', 'category']);
   
    if ($request->has('name')) {
         $visitors = $visitors->whereHas('product', function( $query ) use ( $request ){
                      $query->where('name', $request->name);
                  })->orWhereHas('category', function( $query ) use ( $request ){
                      $query->where('name', $request->name);
                  });
    }
   
    $visitors = $visitors->get();
  
    dd($visitors);
}
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel 6 wherehas use variable.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment