DEV Community

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

Laravel 8 Eloquent orWhereHas Condition

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

For orWhereHas() condition in laravel 8 we are using three tables like users, country, and state, and in the users tabel add the 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() relationship or orwherehas condition in laravel 8.

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();
}
Enter fullscreen mode Exit fullscreen mode

Read More: How To Push Array Element In Node.js


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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Read More: Laravel 8 Eloquent whereHas Condition


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();  
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
timoye profile image
Timothy Soladoye

Nice article
Take note, when using orWhere, be careful as it may conflict the previous where. The or can change the output you are expecting.
You can nest all your or queries inside a

->where(function($q){
});
Enter fullscreen mode Exit fullscreen mode

Example

 User::where(function ($q){
          $q->where('email','like',"%".request()->q."%");
          $q->orwhere('first_name','like',"%".request()->q."%");
          $q->orwhere('last_name','like',"%".request()->q."%");
       });
Enter fullscreen mode Exit fullscreen mode