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.

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.

  Laravel Pivot Tables Tutorial Example

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 :

  Dynamic Autocomplete Textbox in jQuery Ajax

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 Reply

avatar
  Subscribe  
Notify of