ItSolutionStuff.com

Laravel Eloquent without() and withOnly() Method Example

By Hardik Savani • November 5, 2023
Laravel

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:

user_payments:

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;

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 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::without("payments")

->get();

dd($users);

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Hardik Savani

[country_id] => 2

[state_id] => 1

[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

)

)

[1] => Array

(

[id] => 2

[name] => Aatman Infotech

[country_id] => 2

[state_id] => 2

[email] => aatmaninfotech@gmail.com

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

)

)

)

withOnly() Method Example:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

use Illuminate\Database\Eloquent\Builder;

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

[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] => aatmaninfotech@gmail.com

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

[payments] => Array

(

)

)

)

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent orWhereHas() Condition Example

Read Now →

Laravel Eloquent whereRelation() Condition Example

Read Now →

Laravel Eloquent Select Single Column to Array Example

Read Now →

Laravel Eloquent firstWhere() Example

Read Now →

Laravel Eloquent withMin(), withMax() and withAvg() Example

Read Now →

Laravel Eloquent firstOrCreate Example

Read Now →

Laravel Eloquent Delete Record By ID Example

Read Now →

Delete All Records from Table in Laravel Eloquent

Read Now →

Laravel Eloquent whereNull() Query Example

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →

Laravel Has Many Through Eloquent Relationship Tutorial

Read Now →