Exclude Model Attributes Easily with Laravel's New except() Method

Need to get model attributes while excluding sensitive or unnecessary fields? Laravel's new Model::except() method complements the existing only() method by doing exactly the opposite.
When working with Eloquent models, you often need to retrieve a subset of model attributes, perhaps to pass to a view or return in an API response. Laravel has long provided the only() method to whitelist specific attributes, but sometimes it's more convenient to work from the other direction - keeping everything except a few attributes. The new except() method makes this simple and expressive.
Let's see how it works:
$attributes = $user->except('password', 'remember_token');
// Returns all attributes except password and remember_token
Real-World Example
The except() method is particularly useful when preparing models for display or serialization while excluding sensitive or unnecessary fields:
class UserController extends Controller
{
public function show(User $user)
{
// Return all user data except sensitive fields
return view('users.profile', [
'userData' => $user->except('password', 'remember_token', 'two_factor_secret')
]);
}
public function apiProfile(Request $request)
{
$user = $request->user();
// Return all user attributes except internal or sensitive ones
return response()->json(
$user->except('id', 'password', 'remember_token', 'email_verified_at', 'created_at', 'updated_at')
);
}
}
By leveraging the new except() method alongside the existing only() method, you now have a complete toolkit for retrieving exactly the model attributes you need in any situation.
If this guide was helpful to you, subscribe to my daily newsletter and give me a follow on X/Twitter (https://x.com/harrisrafto), Bluesky (https://bsky.app/profile/harrisrafto.eu), and YouTube (https://www.youtube.com/@harrisrafto). It helps a lot!