F
Filament10mo ago
jeph

two columns from a single database column

Hello everyone! Before in v2, I can create two columns from a single database column. But now in v3, when I create a second column from the same database column, the first column will be replaced by the 2nd column. Is there a way to make two columns out of the same database column now in v3? Sample code:
TextColumn::make('name')
->formatStateUsing(function ($state) {
$names = explode(' ', $state); // Split the full name into an array
return $names[0]; // First name
})
->label('First Name'),

TextColumn::make('name')
->formatStateUsing(function ($state) {
$names = explode(' ', $state); // Split the full name into an array
return $names[1] ?? ''; // Last name (or empty string if not available)
})
->label('Last Name'),
TextColumn::make('name')
->formatStateUsing(function ($state) {
$names = explode(' ', $state); // Split the full name into an array
return $names[0]; // First name
})
->label('First Name'),

TextColumn::make('name')
->formatStateUsing(function ($state) {
$names = explode(' ', $state); // Split the full name into an array
return $names[1] ?? ''; // Last name (or empty string if not available)
})
->label('Last Name'),
5 Replies
Zagna
Zagna10mo ago
Virtual columns? $table->string('first_name')->virtualAs('substring_index(name, \' \', 1)'); $table->string('last_name')->virtualAs('substring_index(name, \' \', -1)'); I have no idea if that works, just copypasted stuff
jeph
jephOP10mo ago
i see.. yeah i think that would work as well. i will go that route if there's no way to do it anymore using only filament. before in v2 its as simple as creating two TextColumn::make('name') with different labels.
Craig Potter
Craig Potter10mo ago
Have you tried ->id()
TextColumn::make('name')
->formatStateUsing(function ($state) {
$names = explode(' ', $state); // Split the full name into an array
return $names[0]; // First name
})
->label('First Name')
->id('first_name'),

TextColumn::make('name')
->formatStateUsing(function ($state) {
$names = explode(' ', $state); // Split the full name into an array
return $names[1] ?? ''; // Last name (or empty string if not available)
})
->label('Last Name')
->id('last_name'),
TextColumn::make('name')
->formatStateUsing(function ($state) {
$names = explode(' ', $state); // Split the full name into an array
return $names[0]; // First name
})
->label('First Name')
->id('first_name'),

TextColumn::make('name')
->formatStateUsing(function ($state) {
$names = explode(' ', $state); // Split the full name into an array
return $names[1] ?? ''; // Last name (or empty string if not available)
})
->label('Last Name')
->id('last_name'),
jeph
jephOP10mo ago
sorry ->id( ) did not work Method Filament\Tables\Columns\TextColumn::id does not exist.
Craig Potter
Craig Potter10mo ago
Sorry I mis-read this as TextInput 🙂 Thought it was a form input. Can't you use an accessor on your model for this ? How about
// App/Models/Customer

protected function firstName(): Attribute
{
return Attribute::make(
get: fn (string $value) => Str::of($this->name)->split('/[\s]+/')->first() ?? '',
);
}

protected function lastName(): Attribute
{
return Attribute::make(
get: fn (string $value) => Str::of($this->name)->split('/[\s]+/')->last() ?? '',
);
}

// Your Table

TextColumn::make('first_name'),

TextColumn::make('last_name'),
// App/Models/Customer

protected function firstName(): Attribute
{
return Attribute::make(
get: fn (string $value) => Str::of($this->name)->split('/[\s]+/')->first() ?? '',
);
}

protected function lastName(): Attribute
{
return Attribute::make(
get: fn (string $value) => Str::of($this->name)->split('/[\s]+/')->last() ?? '',
);
}

// Your Table

TextColumn::make('first_name'),

TextColumn::make('last_name'),
Want results from more Discord servers?
Add your server