Laravel 8 Eloquent orWhereHas Condition

Websolutionstuff | Oct-15-2021 | Categories : Laravel

In this example we will see laravel 8 eloquent orWhereHas() condition. In previous example we will learn about laravel 8 whereHas() condition. So, I will give you simple example of how to use orWherehas() condition with laravel eloquent relationship. you can also use with laravel 6, laravel 7 and laravel 8 version.

For orWhereHas() condition in laravel 8 we are using three tables like userscountry and state and in users tabel add country_id and state_id relationship. And also in this use whereHas() with orWhereHas() condition in laravel 8.

So, let's see laravel 8 orWhereHas() realationship.

Example 1 : Laravel orWhereHas()

Laravel eloquent whereHas() method works basically the same as has() but it just allows you to specify additional filters for the related model to check. 

public function index()
{
    $name = 'California';
    $users = User::with('country','state')
                    ->whereHas('country', function ($query) use($name){
                        $query->where('name', 'like', '%'.$name.'%');
                    })
                    ->orWhereHas('state', function ($query) use($name){
                        $query->where('name', 'like', '%'.$name.'%');
                    })
                    ->get()
                    ->toArray();
}

 

 

Example : 2

app/Models/User.php

<?php
  
namespace App\Models;
  
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
  
class User extends Authenticatable
{
    
    protected $fillable = [
        'name',
        'email',
        'password',
    ]; 
  
    public function country()
    {
        return $this->belongsTo(Country::class);
    }
    
    public function state()
    {
        return $this->belongsTo(State::class);
    }
}

app/Models/Country.php

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class Country extends Model
{
    use HasFactory;
}

app/Models/State.php

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class State extends Model
{
    use HasFactory;
}

 

 

app/Http/Controllers/UserController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{
    public function index()
    {
        $name = 'California';
        $users = User::with('country','state')
                        ->whereHas('country', function ($query) use($name){
                            $query->where('name', 'like', '%'.$name.'%');
                        })
                        ->orWhereHas('state', function ($query) use($name){
                            $query->where('name', 'like', '%'.$name.'%');
                        })
                        ->get()
                        ->toArray();  
    }
}

You might also like : 

Recommended Post
Featured Post
How to Install Composer on Ubuntu 22.04
How to Install Composer on Ubu...

Hey there! If you're diving into the world of PHP development on your Ubuntu 22.04 machine, you'll likely come a...

Read More

Jan-10-2024

Node.js Express CRUD Example with MySQL
Node.js Express CRUD Example w...

Hello Guys, In this tutorial we will see how to perform Node js Express CRUD example with mysql. Node js Express ...

Read More

Aug-02-2021

How To Create Dynamic Line Chart In Laravel 9
How To Create Dynamic Line Cha...

In this article, we will see how to create a dynamic line chart in laravel 9. A dynamic line chart or line plot or...

Read More

Mar-22-2022

How to Install PHP DOM Extension in Ubuntu 23.04
How to Install PHP DOM Extensi...

In this tutorial, I will guide you through the process of installing the PHP DOM Extension in Ubuntu 23.04. The PHP DOM...

Read More

Jan-29-2024