Laravel 6 Eloquent Has With and WhereHas

Laravel Mysql Mysqli php Programming Technology

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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   laravel permissions Tutorial with Example
  2.   Laravel 6 Guzzle PHP HTTP client Request
  3.   How to Set Headers in Laravel 6 Example?
  4.   PHP Facebook Graph API GET comments hide-delete comments
  5.   Laravel WhereHas then orWhereHas Example
  6.   One to Many Relationship in Laravel 5.7 Eloquent
  7.   Polymorphic relationships in Laravel 5.7 Example
  8.   Simple Eloquent Laravel One to One Relationship
  9.   how to check a plugin is activated or not in wordpress
  10.   Polymorphic One to Many Relationship Example

Leave a Reply

Your email address will not be published. Required fields are marked *