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.