Laravel Eloquent whereRelation() Condition Example
If you need to see example of laravel whereRelation condition example. if you have question about laravel relationship whereRelation then i will give simple example with solution. if you want to see example of laravel whereRelation relationship example then you are a right place. Here you will learn laravel relationship where condition. Follow bellow tutorial step of laravel where relationship column.
Here, i will give you very simple example of how to use whereRelation condition with laravel eloquent relationship. you can also use with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
Sometime we need to add where condition with relation table then you need to use whereRelation() method. for example if you have users with country relation then you want to filter with country then you must have to use whereHas(). so here i will give you two way to add condition with relation model, one using whereHas and second using whereRelation().
Here, you can see simple example and then you can also see full example with output:
Example 1: Laravel whereRelation()
public function index()
{
$name = 'india';
$users = User::with('country')
->whereRelation('country', 'name', 'like', '%'.$name.'%')
->get()
->toArray();
dd($users);
}
Example 2: Laravel whereHas()
public function index()
{
$name = 'india';
$users = User::with('country')
->whereHas('country', function (Builder $query) use($name){
$query->where('name', 'like', '%'.$name.'%');
})
->get()
->toArray();
dd($users);
}
Full Example:
Table Data with Screenshot:
users:
countries:
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);
}
}
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;
}
Controller Code:
app/Http/Controllers/DemoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$name = 'india';
$users = User::with('country')
->whereRelation('country', 'name', 'like', '%'.$name.'%')
->get()
->toArray();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[id] => 1
[name] => Hardik Savani
[country_id] => 2
[email] => savanihd@gmail.com
[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
)
)
)
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Order By Relationship Sum Column Example
- Laravel Relationship Eager Loading with Condition Example
- Laravel Relationship Eager Loading with Count Example
- Laravel Relationship Where Condition Example
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel One to One Eloquent Relationship Tutorial
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel One to Many Polymorphic Relationship Tutorial
- Laravel Many to Many Polymorphic Relationship Tutorial