Laravel Eloquent orWhereHas() Condition Example

By Hardik Savani September 10, 2021 Category : Laravel

Hello,

This is a short guide on laravel orwherehas condition example. this example will help you laravel relationship orwherehas. let’s discuss about laravel orwherehas relationship example. Here you will learn laravel relationship or where condition.

Here, i will give you very 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.

Sometime we need to add or where condition with relation table then you need to use orwherehas() method. for example if you have users with country relation and city relation then you want to filter with country or city then you must have to use whereHas() and orWhereHas(). so here i will give you simple example with relation model.

Here, you can see simple example and then you can also see full example with output:

Laravel orWhereHas()

public function index()

{

$name = 'gu';

$users = User::with('country', 'state')

->whereHas('country', function (Builder $query) use($name){

$query->where('name', 'like', '%'.$name.'%');

})

->orWhereHas('state', function (Builder $query) use($name){

$query->where('name', 'like', '%'.$name.'%');

})

->get()

->toArray();

dd($users);

}

Full Example:

Table Data with Screenshot:

users:

countries:

states:

Models Code:

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;

use Illuminate\Notifications\Notifiable;

use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable

{

use HasApiTokens, HasFactory, Notifiable;

/**

* The attributes that are mass assignable.

*

* @var string[]

*/

protected $fillable = [

'name',

'email',

'password',

];

/**

* The attributes that should be hidden for serialization.

*

* @var array

*/

protected $hidden = [

'password',

'remember_token',

];

/**

* The attributes that should be cast.

*

* @var array

*/

protected $casts = [

'email_verified_at' => 'datetime',

];

/**

* Get the comments for the blog post.

*/

public function country()

{

return $this->belongsTo(Country::class);

}

/**

* Get the comments for the blog post.

*/

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;

}

Controller Code:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

use Illuminate\Database\Eloquent\Builder;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$name = 'gu';

$users = User::with('country', 'state')

->whereHas('country', function (Builder $query) use($name){

$query->where('name', 'like', '%'.$name.'%');

})

->orWhereHas('state', function (Builder $query) use($name){

$query->where('name', 'like', '%'.$name.'%');

})

->get()

->toArray();

dd($users);

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Hardik Savani

[country_id] => 2

[state_id] => 1

[email] => [email protected]

[email_verified_at] =>

[two_factor_secret] =>

[two_factor_recovery_codes] =>

[current_team_id] =>

[profile_photo_path] =>

[created_at] => 2020-09-12T06:46:08.000000Z

[updated_at] => 2020-09-18T12:04:09.000000Z

[deleted_at] =>

[country] => Array

(

[id] => 2

[name] => india

[code] => 2

[created_at] => 2021-08-09T14:58:47.000000Z

[updated_at] => 2021-08-09T14:58:47.000000Z

)

[state] => Array

(

[id] => 1

[name] => Gujarat

[created_at] =>

[updated_at] =>

)

)

)

i hope it can help you...

Tags :