Level 24
If you want to add the computed column to every single query, you can use a global scope: https://laravel.com/docs/eloquent#global-scopes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Is it possible to always retrieve an Eloquent model with extra column? What I mean is for example
SELECT id, name, LENGTH (name) as length, created_at FROM users WHERE ...
So I can use it in query builder for sorting etc.
Also would get implicit column on a model
$user->length
Right now, I am just wrapping simple select workaround like that:
self::select('*', DB::raw('LENGTH (name) as length'))
But it's not entirely good looking imo.
Or is that way too much of a Data Mapper approach in the Active Record based Eloquent?
Just a quick note: Accessor doesn't solve my problem.
Please or to participate in this conversation.