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.

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.   install PHP Accelerator in Xampp - prestashop
  2.   Laravel Many to many relationships with itself
  3.   How to Create Model In Laravel 6 Example?
  4.   One to Many Relationship in Laravel 5.7 Eloquent
  5.   JavaScript Dynamic Input Form Example
  6.   Polymorphic One to Many Relationship Example
  7.   Angular 9/8 Radio Button on Form Submit
  8.   Laravel Pivot Tables Tutorial Example
  9.   Redirect HTTP To HTTPS Using Laravel Middleware
  10.   Laravel 6.2 Eloquent Relationship Tutorial

Leave a Reply

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