I’m working on a Laravel (and FilamentPHP) project and noticed a discrepancy between the old “magic” accessor method and the new class-based Attribute accessor introduced in Laravel 8.40+. Specifically, when I use:
Model::query()->pluck('name', 'id')
- The old magic accessor (
getNameAttribute()
) applieducwords()
to the plucked values. - The new class-based accessor (
protected function name(): Attribute
) does not apply the transformation when plucking.
Here’s a simplified example of my model:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentCastsAttribute;
use IlluminateDatabaseEloquentBuilder;
use IlluminateDatabaseEloquentModel;
use AppEnumsSomeEnumStatusState;
class MyModel extends Model
{
// OLD Magic Accessor (works with pluck)
// public function getNameAttribute($value)
// {
// return ucwords(strtolower($value));
// }
// NEW Class-based Accessor (not applying with pluck)
protected function name(): Attribute
{
return Attribute::make(
get: fn ($value) => ucwords(strtolower($value))
);
}
public function scopeOpen(Builder $query): Builder
{
return $query->where('state', SomeEnumStatusState::OPEN);
}
protected function casts(): array
{
return [
'state' => SomeEnumStatusState::class,
];
}
}
And in my FilamentPHP Resource:
FormsComponentsSelect::make('model_id')
->label('Status')
->relationship('modelRelationship', 'name')
->options(MyModel::query()->pluck('name', 'id')),
My questions are:
-
Why does
Model::query()->pluck('name', 'id')
return the raw database values when using the new class-basedname(): Attribute
but returns transformed values under the oldgetNameAttribute()
method? -
Is this the intended behavior, or am I missing something about how
pluck()
interacts with the newer Attribute accessors? -
What is the recommended or “best practice” way to retrieve transformed attribute values (especially with FilamentPHP) under the new accessor approach?
Any insights, explanations, or code examples showing how to ensure pluck()
respects the class-based accessor would be greatly appreciated!
2
Answers
AFAIK, pluck doesn’t access your model but just gets the columns (and only those) directly from the database. This has a significant performance advantage, since much less data is retrieved from the database, and Laravel doesn’t need to construct the model objects. The disadvantage however, is that you get only the literal database values which may or may not suffice your needs.
Hence when using pluck on the query, your model isn’t constructed/booted and thus your accessor isn’t used. If you want to retrieve those attribute values, you can do something like
This retrieves all records of this model from the database, constructs all the models and puts it in a collection and then gets the id and name.
Looking into the vendor files for this, the
pluck
method respects mutators as follows:Taking a dive deeper into the
hasGetMutator
method we can see that this will only check for method names using the previous magic methods.One way i’ve found to get around this would be to specify the attribute on the
$appends
array (relevant docs) on the model and then->get()
before->pluck(...)
like so: