DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

Laravel 8 Eloquent whereHas Condition

In this article, we will see laravel 8 eloquent whereHas() condition. You will learn about wherehas() condition in laravel 8. I will give you simple example of how to use wherehas() condition with laravel 8 eloquent relationship. You can also use the laravel 6, laravel 7, laravel 8, and laravel 9 version.

For wherehas condition in laravel 8 we need two table like users and country and in users tabel add country_id.

So, let's see, laravel 8 wherehas query or laravel 8 wherehas relationship.

Example 1 : Laravel whereHas()

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 = 'india';
    $users = User::with('country')
                    ->whereHas('country', function ($query) use($name){
                        $query->where('name', 'like', '%'.$name.'%');
                    })
                    ->get()
                    ->toArray();
}
Enter fullscreen mode Exit fullscreen mode

Read Also: How To Store Data In Database Using Node.js


Example 2 : Laravel has()

Laravel eloquent has() method is used to filter the selecting model based on the selected relationship. It works similarly to where method for relations.

use App\Models\User;

$users = User::has('country')->get();
Enter fullscreen mode Exit fullscreen mode

You may also specify an operator and count value to further customize the query:

$posts = Post::has('comments', '>=', 3)->get();
Enter fullscreen mode Exit fullscreen mode

Example : 3

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);
    }
}
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/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 = 'india';
        $users = User::with('country')
                        ->whereHas('country', function ($query) use($name){
                            $query->where('name', 'like', '%'.$name.'%');
                        })
                        ->get()
                        ->toArray();  
    }
}
Enter fullscreen mode Exit fullscreen mode

You might also like:

Read Also: Laravel whereIn and whereNotIn Query Example

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈