Get Laravel Models with All Attributes
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Comprehensive Guide to Retrieving Laravel Models with All Attributes - Including Null Values
Body:
Introduction
In the world of Laravel, models are a way to interact with your database objects. However, sometimes there may be a need to load all attributes from a model even if they are null. This is particularly important when building functions that update models using arrays containing attribute values, needing to account for both existing and new properties. In this article, we will discuss how you can retrieve Laravel models with all their attributes, including null ones, and provide best practices for handling them effectively in your codebase.Retrieving Models with All Attributes
To begin with, let's see how you would typically fetch a model's attributes using the Laravel way. The `find()` method on the Eloquent model class gets an object for a given ID. When calling this method, it will only return the attributes that are not null. In our case, we want to retrieve models with all their attributes - even if they have null values.Laravel provides a helper function called with() that can be used for this purpose. Using with(), you can specify a set of relationship methods explicitly on an Eloquent model. Let's see how it works:
$user = User::with(['phones', 'emails'])->firstOrFail($userId);This code snippet loads the user with id=$userId and eager-loads associated phone numbers (through relation "phones") and email addresses (relation "emails"). In our case, we want to load all attributes for a particular model regardless of their value. Thankfully, there is another helper function called withTrashed().
Using withTrashed()
The `withTrashed()` method can be used in conjunction with the `findOrFail()` or `firstOrFail()` methods to load your model along with all its attributes, including those that are set to null:$user = User::withTrashed()->findOrFail($userId);Here, the controller will fetch a specific user with all their attributes in the state they currently exist. However, due to Laravel's default behaviour, if a field is set to null but not explicitly specified for retrieval with findOrFail(), that attribute won't be loaded from the database. To force loading all fields, you can use the pluck() method:
$user = User::withTrashed()->findOrFail($userId);
$rawUserData = $user->getAttributes();
$userWithNullValues = array_merge(collect($rawUserData), collect($user)->only([
'id',
'name',
'email'
]));This code block stores the loaded model as $user, grabs all its attributes and then merges them with those from a freshly created collection of the same model. The second array is created using Laravel's only() method, which specifies which attributes to include in the output. Finally, we merge both collections to form a new one with all model attributes, including null values.