Laravel Eloquent without() and withOnly() Method Example

This article will provide some of the most important example laravel eloquent without and withonly method. Here you will learn laravel eloquent relationships without method. In this article, we will implement a laravel eloquent relationships withonly method. This post will give you simple example of laravel without and withonly eloquent method.

Here, i will explain you how to use without() and withOnly() method of laravel relation eloquent method.

Both method we can use with model $with config variable. we define getting all users with default payments and country with $with config variable of user model. but sometime you need users list without payments then you can use without() and sometime you need only country then you can use withOnly() method.

So, let’s see bellow simple example and you will got it.

Table Data with Screenshot:

users:

Laravel Eloquent without() and withOnly() Method Example

user_payments:

Laravel Eloquent without() and withOnly() Method Example

countries:

Laravel Eloquent without() and withOnly() Method Example

Models Code:

app/Models/User.php

<?php

namespace AppModels;

use IlluminateContractsAuthMustVerifyEmail;

use IlluminateDatabaseEloquentFactoriesHasFactory;

use IlluminateFoundationAuthUser as Authenticatable;

use IlluminateNotificationsNotifiable;

use LaravelSanctumHasApiTokens;

class User extends Authenticatable

{

use HasApiTokens, HasFactory, Notifiable;

protected $with = ['payments', 'country'];

/**

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

{

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

}

}

without() Method Example:

app/Http/Controllers/DemoController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsUser;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$users = User::without("payments")

->get();

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

)

)

[1] => Array

(

[id] => 2

[name] => Aatman Infotech

[country_id] => 2

[state_id] => 2

[email] => [email protected]

[email_verified_at] =>

[two_factor_secret] =>

[two_factor_recovery_codes] =>

[current_team_id] =>

[profile_photo_path] =>

[created_at] => 2020-09-30T13:33:52.000000Z

[updated_at] => 2020-09-30T13:33:52.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

)

)

[2] => Array

(

[id] => 3

[name] => Haresh

[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

)

)

)

withOnly() Method Example:

app/Http/Controllers/DemoController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsUser;

use IlluminateDatabaseEloquentBuilder;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$users = User::withOnly("payments")

->get();

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] =>

[payments] => Array

(

[0] => Array

(

[id] => 1

[user_id] => 1

[charge] => 50

[payment_date] => 2021-08-04

[created_at] =>

[updated_at] =>

)

)

)

[1] => Array

(

[id] => 2

[name] => Aatman Infotech

[country_id] => 2

[state_id] => 2

[email] => [email protected]

[email_verified_at] =>

[two_factor_secret] =>

[two_factor_recovery_codes] =>

[current_team_id] =>

[profile_photo_path] =>

[created_at] => 2020-09-30T13:33:52.000000Z

[updated_at] => 2020-09-30T13:33:52.000000Z

[deleted_at] =>

[payments] => Array

(

[0] => Array

(

[id] => 2

[user_id] => 2

[charge] => 45

[payment_date] => 2021-09-07

[created_at] =>

[updated_at] =>

)

)

)

[2] => Array

(

[id] => 3

[name] => Haresh

[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] =>

[payments] => Array

(

)

)

)

I hope it can help you….

Leave a Comment