Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rudexpunx's avatar

Retrieving extra column (computed) in Eloquent model

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.

0 likes
1 reply

Please or to participate in this conversation.