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

gabriel27's avatar

Laravel Accessor to get full name

Hello,

I have 2 columns: first_name and last_name.

I searched in the forum and found only old solutions with the accessors and they did not work.

My latest attempt is to add in the model as per the Laravel docs:

protected function fullName(): Attribute
    {
        return new Attribute(
            get: fn () => $this->name .' ' . $this->surname
        );
    }

and in the Filament resource table:

                Tables\Columns\TextColumn::make('user.full_name')
                    ->searchable()
                    ->sortable(),

I did not mention that I need to get this via a relationship. I am not in the UserResource. I am in another resource that has a relationship. That is why I set user.full_name

The column shows empty.

As I mentioned I also tried the old way:

    public function getFullNameAttribute()
    {
    	return ucwords("{$this->name} {$this->surname}");
    }

Again empty.

Is there any way I may get in the table from Filament concatenated columns?

Thank you

0 likes
5 replies
LaryAI's avatar
Level 58

It seems like there might be a slight confusion in the property names used in the accessor method and the actual column names in the database. Based on your description, you have first_name and last_name columns, but in your accessor, you are using $this->name and $this->surname. Make sure that the property names match the column names in your database.

Here's how you can define an accessor in your Eloquent model to concatenate the first_name and last_name columns:

class User extends Model
{
    // ...

    public function getFullNameAttribute()
    {
        return "{$this->first_name} {$this->last_name}";
    }

    // ...
}

With this accessor, you can now access the full name as a property on your User model instances like so: $user->full_name.

In your Filament resource table, you should be able to use the accessor like this:

Tables\Columns\TextColumn::make('full_name')
    ->searchable()
    ->sortable(),

Make sure that you are using the correct attribute name in the make method. Since the accessor is named getFullNameAttribute, you should use full_name as the attribute name in the make method.

If you are using Laravel 8 or later and want to use the new Eloquent Accessor & Mutator syntax, you can define the accessor like this:

use Illuminate\Database\Eloquent\Casts\Attribute;

class User extends Model
{
    // ...

    protected function fullName(): Attribute
    {
        return Attribute::make(
            get: fn ($value, $attributes) => $attributes['first_name'] . ' ' . $attributes['last_name']
        );
    }

    // ...
}

Again, make sure to use the correct attribute name in your Filament resource table:

Tables\Columns\TextColumn::make('full_name')
    ->searchable()
    ->sortable(),

If you still encounter issues, double-check that your User model is correctly linked to your Filament resource and that there are no typos or mismatches in your column names.

gabriel27's avatar
gabriel27
OP
Best Answer
Level 1

Hello,

Both worked in the end.

I found that if the field in the DB is called user_id and in the model the relationship is called something else like:

    public function person(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

It will not work. The relationship has to be user if the field is user_id. I hope it helps.

juliofa's avatar

Hi there,

I'm facing the same issue. Listing works fine, but as soon as I try to sort by a computed column, I get the following error:

SQLSTATE[42703]: Undefined column: 7 ERROR: column "full_name" does not exist LINE 1: ...sers" where "users"."deleted_at" is null order by "full_name... ^ (Connection: pgsql, SQL: select * from "users" where "users"."deleted_at" is null order by "full_name" asc limit 25 offset 0)

For reference, I have the following in my User model:

public function getFullNameAttribute()
{
    return "{$this->lastname}, {$this->firstname}";
}

And this in my table:

            Tables\Columns\TextColumn::make('full_name')
                ->label('Nombre completo')
                ->sortable()
                ->searchable(),

Thanks in advance! Is the default view of names that I need for the whole project.

gabriel27's avatar

Hello,

You may not order using a custom attribute as it does not exist in the DB table.

You may order in the sql query based on the component fields (e.g. firstname, lastname).

You may do the query, get the results in a collection and try using the sort() method.

I hope the above helps.

miaalexandra's avatar

Working with Laravel's Eloquent ORM, you might come across situations where you want to treat some attributes of your models as specific data types instead of raw strings. Laravel provides a feature called “casting” that allows you to automatically transform attribute data into desired datatypes.Type casting, also known as type conversion, is an important concept in Java that allows us to convert one data type into another. It is useful when we need to perform operations on different data types or when you want to store a value of one data type into a variable of another data type

Please or to participate in this conversation.