Skip to content

Commit 6ca9dfa

Browse files
committed
refactor: use hard copy for user's full name instead of composite attribute
1 parent c7408e0 commit 6ca9dfa

4 files changed

Lines changed: 20 additions & 95 deletions

File tree

database/migrations/0001_01_01_000000_create_users_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function up(): void
1313
{
1414
Schema::create('users', function (Blueprint $table) {
1515
$table->id();
16+
$table->string('name')->nullable();
1617
$table->string('first_name')->nullable();
1718
$table->string('last_name')->nullable();
1819
$table->string('email')->unique();

src/Filament/Resources/UserResource.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ public static function table(Table $table): Table
8787
->searchable()
8888
->sortable()
8989
->toggleable(),
90-
Tables\Columns\TextColumn::make('full_name')
91-
->searchable(query: function (Builder $query, string $search): Builder {
92-
return $query->whereRaw(User::getCompositeDefinition('full_name').' LIKE ?', ["%$search%"]);
93-
})
90+
Tables\Columns\TextColumn::make('name')
91+
->label('Full name')
92+
->searchable()
9493
->sortable()
9594
->toggleable(),
9695
];
@@ -148,7 +147,12 @@ public static function table(Table $table): Table
148147

149148
$filters[] = Tables\Filters\QueryBuilder::make()
150149
->constraints([
151-
TextConstraint::make('first_name'),
150+
TextConstraint::make('first_name')
151+
->label('First name'),
152+
TextConstraint::make('last_name')
153+
->label('Last name'),
154+
TextConstraint::make('name')
155+
->label('Full name'),
152156
]);
153157

154158
return $table
@@ -200,7 +204,7 @@ public static function infolist(Infolist $infolist): Infolist
200204
->circular(),
201205
Group::make()
202206
->schema([
203-
TextEntry::make('full_name')
207+
TextEntry::make('name')
204208
->label('Full name'),
205209
TextEntry::make('email')
206210
->icon(config('eclipse.email_verification') ? fn (User $user) => $user->email_verified_at ? 'heroicon-s-check-circle' : 'heroicon-s-x-circle' : null)

src/Foundation/Model/HasCompositeAttributes.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/Models/User.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Eclipse\Core\Models;
44

55
use Eclipse\Core\Database\Factories\UserFactory;
6-
use Eclipse\Core\Foundation\Model\HasCompositeAttributes;
76
use Filament\Models\Contracts\FilamentUser;
87
use Filament\Models\Contracts\HasAvatar;
98
use Filament\Models\Contracts\HasName;
@@ -14,13 +13,13 @@
1413
use Illuminate\Foundation\Auth\User as Authenticatable;
1514
use Illuminate\Notifications\Notifiable;
1615
use Illuminate\Support\Collection;
17-
use Illuminate\Support\Facades\DB;
1816
use Spatie\MediaLibrary\HasMedia;
1917
use Spatie\MediaLibrary\InteractsWithMedia;
2018
use Spatie\Permission\Traits\HasRoles;
2119

2220
/**
2321
* @property int $id
22+
* @property-read string|null $name
2423
* @property string|null $first_name
2524
* @property string|null $last_name
2625
* @property string $email
@@ -29,11 +28,10 @@
2928
* @property string|null $remember_token
3029
* @property string|null $created_at
3130
* @property string|null $updated_at
32-
* @property-read string $full_name User's full name
3331
*/
3432
class User extends Authenticatable implements FilamentUser, HasAvatar, HasMedia, HasName, HasTenants
3533
{
36-
use HasCompositeAttributes, HasFactory, HasRoles, InteractsWithMedia, Notifiable;
34+
use HasFactory, HasRoles, InteractsWithMedia, Notifiable;
3735

3836
protected $table = 'users';
3937

@@ -92,20 +90,6 @@ public function getFilamentName(): string
9290
return "$this->first_name $this->last_name";
9391
}
9492

95-
protected static function defineCompositeAttributes(): array
96-
{
97-
switch (DB::getDriverName()) {
98-
case 'sqlite':
99-
return [
100-
'full_name' => "users.first_name || ' ' || users.last_name",
101-
];
102-
default:
103-
return [
104-
'full_name' => "TRIM(CONCAT(IFNULL(users.first_name, ''), ' ', IFNULL(users.last_name, '')))",
105-
];
106-
}
107-
}
108-
10993
public function canAccessTenant(Model $tenant): bool
11094
{
11195
return $this->sites()->whereKey($tenant)->exists();
@@ -125,4 +109,11 @@ protected static function newFactory()
125109
{
126110
return UserFactory::new();
127111
}
112+
113+
protected static function booted()
114+
{
115+
static::saving(function (self $user) {
116+
$user->name = trim("$user->first_name $user->last_name");
117+
});
118+
}
128119
}

0 commit comments

Comments
 (0)