Laravel: Remove an attribute in returned result

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Effortlessly Remove Unwanted Attributes from Laravel Collection Results Laravel's Eloquent model gives us the ability to easily interact with our database. In this blog post, we will explore how to remove unwanted attributes from a returned collection without hardcoding or compromising Laravel's default behavior. This method can be done using the Laravel Collection class itself, making it more convenient and maintainable than raw PHP operations. To get started, let's assume we have our orders model as follows:
$orders = Order::all();
return $orders;
The returned order collection will contain attributes that are not desired, such as 'qr_code'. To remove this attribute from the collection, you can use Laravel's Collection class method 'only'. This method accepts an array of allowed attributes and will return a new collection containing only those specified attributes. In this case, our intended result would be a collection with only 'id' and 'qr_code_url' fields:
use Illuminate\Support\Collection;

$orders = Order::all();
$collectionWithoutQrCodeAttribute = $orders->map(function ($order) {
    return collect($order)->only(['id', 'qr_code_url']);
});
return $collectionWithoutQrCodeAttribute;
Here, we first mapped the original collection to a new Collection instance and used the only method to specify which attributes should be included in the new collection. It's essential to use `collect($order)` before calling the 'only' method on each item to ensure you are working with a new Laravel Collection object. Alternatively, if you don't want to remove any specific attribute but need to hide them from the returned response entirely, you can use Laravel's Collection class method 'hidden'. This method accepts an array of hidden attributes that will be omitted from the serialized representation of a collection:
use Illuminate\Support\Collection;

$orders = Order::all();
$collectionWithoutHiddenQrCodeAttribute = $orders->map(function ($order) {
    return collect($order)->hidden(['qr_code']);
});
return $collectionWithoutHiddenQrCodeAttribute;
In this example, we've used the hidden method with the 'qr_code' attribute inside an array to hide it from the serialized collection. This ensures that the unwanted data is not available in the returned output while preserving its existence within the database model. Remember that removing or hiding attributes using these methods will not affect any underlying database tables or models, but only modify the response sent to the client. If you need to remove or hide data permanently, consider modifying your Eloquent model or migration files instead of relying on PHP code. In summary, Laravel's Collection class provides several useful methods for manipulating and filtering collections returned by your models. By following best practices and using the 'only' and 'hidden' methods, you can safely remove unwanted attributes from your results without introducing additional complexity or sacrificing maintainability.