Laravel Eloquent doesntHave() Condition Example

Laravel Eloquent doesntHave() Condition Example

In this example we will show laravel Eloquent doesntHave condition example. You will learn how to use laravel doesntHave condition. I would like to share with you simple example of laravel doesntHave example.

Here, We will show laravel doesntHave condition, laravel Eloquent doesntHave column, how to use doesntHave condition with laravel eloquent. so here i will give you simple two example one will doesntHave() and whereDoesntHave().

Example 1: Laravel doesntHave()

public function index()
{
    $users = User::select("*")
                        ->doesntHave("payments")
                        ->get();
  
    dd($users);
}

Example 2: Laravel whereDoesntHave()

public function index()
{
    $month = '09';
    $users = User::select("*")
                    ->whereDoesntHave('payments', function (Builder $query) use($month){
                        $query->whereMonth('payment_date', $month);
                    })
                    ->get();
  
    dd($users);
}

I hope it can help you..

Read Also :  Laravel 8 One to Many Eloquent Relationship Tutorial