Laravel Eloquent doesntHave() Condition Example

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

I am going to explain you lesson of laravel doesntHave condition lesson. if you have question about laravel relationship doesntHave then i will give simple lesson with solution. This post will give you simple lesson of laravel doesntHave relationship lesson. We will use laravel relationship doesntHave condition. Follow bellow tutorial step of laravel doesntHave relationship column.

Hello Brother, i will give you very simple lesson of how to use doesntHave condition with laravel eloquent relationship. you can also use with laravel 6, laravel 7 and laravel 8 version.

Sometime we need to get only records that does not have records on relation table. for lesson i need to get only that users that does not paid. so here i will give you simple two lesson one will doesntHave() and whereDoesntHave().

Hello Brother, you can see simple lesson and then you can also see full lesson with output:

Lesson 1: Laravel doesntHave()

public function index()

{

$users = User::select("*")

->doesntHave("payments")

->get();

dd($users);

}

Lesson 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);

}

Full Example:

Table Data with Screenshot:

users:

user_payments:

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 payments()

{

return $this->hasMany(UserPayment::class);

}

}

app/Models/UserPayment.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class UserPayment extends Model

{

use HasFactory;

}

Lesson 1: 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()

{

$users = User::select("*")

->doesntHave("payments")

->get();

dd($users);

}

}

Result:-

Array

(

[0] => Array

(

[id] => 3

[name] => Haresh

[country_id] => 2

[state_id] => 1

[email] => savanihd2@gmail.com

[email_verified_at] =>

[current_team_id] =>

[profile_photo_path] =>

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

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

[deleted_at] =>

)

)

Lesson 2: 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()

{

$month = '09';

$users = User::select("*")

->whereDoesntHave('payments', function (Builder $query) use($month){

$query->whereMonth('payment_date', $month);

})

->get();

dd($users);

}

}

Result:-

Array

(

[0] => Array

(

[id] => 1

[name] => Sagor Savani

[country_id] => 2

[state_id] => 1

[email] => savanihd@gmail.com

[email_verified_at] =>

[current_team_id] =>

[profile_photo_path] =>

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

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

[deleted_at] =>

)

[1] => Array

(

[id] => 3

[name] => Haresh

[country_id] => 2

[state_id] => 1

[email] => savanihd2@gmail.com

[email_verified_at] =>

[current_team_id] =>

[profile_photo_path] =>

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

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

[deleted_at] =>

)

)

thank you very much for following up with me.

Tags

Recent Posts

Related Posts