From 7b44b07e30e91a10b4b092ef50aed5ee90c7c24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Tue, 28 May 2024 15:56:33 +0200 Subject: [PATCH 01/15] Reorder properties and methods --- packages/admin/src/Models/Staff.php | 60 +++++++++++++++-------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index a0fe5f314b..54dbb01624 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -19,13 +19,7 @@ class Staff extends Authenticatable implements FilamentUser, HasName use Notifiable; use SoftDeletes; - /** - * Return a new factory instance for the model. - */ - protected static function newFactory(): StaffFactory - { - return StaffFactory::new(); - } + protected $guard_name = 'staff'; /** * The attributes that are mass assignable. @@ -40,7 +34,15 @@ protected static function newFactory(): StaffFactory 'password', ]; - protected $guard_name = 'staff'; + /** + * The attributes that should be cast to native types. + * + * @var array + */ + protected $casts = [ + 'email_verified_at' => 'datetime', + 'password' => 'hashed', + ]; /** * The attributes that should be hidden for arrays. @@ -53,21 +55,21 @@ protected static function newFactory(): StaffFactory ]; /** - * The attributes that should be cast to native types. + * Append attributes to the model. * * @var array */ - protected $casts = [ - 'email_verified_at' => 'datetime', - 'password' => 'hashed', + protected $appends = [ + 'fullName', ]; /** - * Append attributes to the model. - * - * @var array + * Get staff member's full name. */ - protected $appends = ['fullName']; + public function getFullNameAttribute(): string + { + return $this->firstname.' '.$this->lastname; + } /** * Create a new instance of the Model. @@ -84,18 +86,11 @@ public function __construct(array $attributes = []) } /** - * Retrieve the model for a bound value. - * - * Currently Livewire doesn't support route bindings for - * soft deleted models so we need to rewire it here. - * - * @param mixed $value - * @param string|null $field - * @return \Illuminate\Database\Eloquent\Model|null + * Return a new factory instance for the model. */ - public function resolveRouteBinding($value, $field = null) + protected static function newFactory(): StaffFactory { - return $this->resolveSoftDeletableRouteBinding($value, $field); + return StaffFactory::new(); } /** @@ -117,11 +112,18 @@ public function scopeSearch($query, $term) } /** - * Get staff member's full name. + * Retrieve the model for a bound value. + * + * Currently Livewire doesn't support route bindings for + * soft deleted models so we need to rewire it here. + * + * @param mixed $value + * @param string|null $field + * @return \Illuminate\Database\Eloquent\Model|null */ - public function getFullNameAttribute(): string + public function resolveRouteBinding($value, $field = null) { - return $this->firstname.' '.$this->lastname; + return $this->resolveSoftDeletableRouteBinding($value, $field); } public function canAccessPanel(Panel $panel): bool From 1fc37e31b72a0fed7b43cd21a7b24b42a12144d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Tue, 28 May 2024 15:57:25 +0200 Subject: [PATCH 02/15] Remove custom route model binding https://github.com/livewire/livewire/pull/7913 --- packages/admin/src/Models/Staff.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 54dbb01624..d5076d5e8c 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -111,21 +111,6 @@ public function scopeSearch($query, $term) } } - /** - * Retrieve the model for a bound value. - * - * Currently Livewire doesn't support route bindings for - * soft deleted models so we need to rewire it here. - * - * @param mixed $value - * @param string|null $field - * @return \Illuminate\Database\Eloquent\Model|null - */ - public function resolveRouteBinding($value, $field = null) - { - return $this->resolveSoftDeletableRouteBinding($value, $field); - } - public function canAccessPanel(Panel $panel): bool { return true; From c79f4c093e1fc10a434583fbd2e9189b4da64a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Tue, 28 May 2024 16:07:09 +0200 Subject: [PATCH 03/15] Remove docblocks --- packages/admin/src/Models/Staff.php | 36 ----------------------------- 1 file changed, 36 deletions(-) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index d5076d5e8c..99df5ba241 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -21,11 +21,6 @@ class Staff extends Authenticatable implements FilamentUser, HasName protected $guard_name = 'staff'; - /** - * The attributes that are mass assignable. - * - * @var array - */ protected $fillable = [ 'firstname', 'lastname', @@ -34,46 +29,25 @@ class Staff extends Authenticatable implements FilamentUser, HasName 'password', ]; - /** - * The attributes that should be cast to native types. - * - * @var array - */ protected $casts = [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; - /** - * The attributes that should be hidden for arrays. - * - * @var array - */ protected $hidden = [ 'password', 'remember_token', ]; - /** - * Append attributes to the model. - * - * @var array - */ protected $appends = [ 'fullName', ]; - /** - * Get staff member's full name. - */ public function getFullNameAttribute(): string { return $this->firstname.' '.$this->lastname; } - /** - * Create a new instance of the Model. - */ public function __construct(array $attributes = []) { parent::__construct($attributes); @@ -85,21 +59,11 @@ public function __construct(array $attributes = []) } } - /** - * Return a new factory instance for the model. - */ protected static function newFactory(): StaffFactory { return StaffFactory::new(); } - /** - * Apply the basic search scope to a given Eloquent query builder. - * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param string $term - * @return void - */ public function scopeSearch($query, $term) { if ($term) { From 28740edd403ff1b7e4b26301dbdb6477a08d0f47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Tue, 28 May 2024 16:21:56 +0200 Subject: [PATCH 04/15] Add missing cast --- packages/admin/src/Models/Staff.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 99df5ba241..7339e9d40d 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -30,6 +30,7 @@ class Staff extends Authenticatable implements FilamentUser, HasName ]; protected $casts = [ + 'admin' => 'bool', 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; From 5f546712e6077a8883c18f034b838dd756122448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Tue, 28 May 2024 16:22:25 +0200 Subject: [PATCH 05/15] Update `full_name` accessor --- packages/admin/src/Models/Staff.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 7339e9d40d..58e980e2ec 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -44,9 +44,11 @@ class Staff extends Authenticatable implements FilamentUser, HasName 'fullName', ]; - public function getFullNameAttribute(): string + protected function fullName(): Attribute { - return $this->firstname.' '.$this->lastname; + return Attribute::get( + fn (): string => "{$this->firstname} {$this->lastname}", + ); } public function __construct(array $attributes = []) From 89cd30b6e0b0fb0d6709555c1ed8afe19fb280e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Tue, 28 May 2024 16:23:36 +0200 Subject: [PATCH 06/15] Update local search scope --- packages/admin/src/Models/Staff.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 58e980e2ec..0f8181fdf6 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -67,14 +67,14 @@ protected static function newFactory(): StaffFactory return StaffFactory::new(); } - public function scopeSearch($query, $term) + public function scopeSearch(Builder $query, ?string $terms): void { - if ($term) { - $parts = explode(' ', $term); + if (! $terms) { + return; + } - foreach ($parts as $part) { - $query->whereAny(['email', 'firstname', 'lastname'], 'LIKE', "%$part%"); - } + foreach (explode(' ', $terms) as $term) { + $query->whereAny(['email', 'firstname', 'lastname'], 'LIKE', "%{$term}%"); } } From 98e44b2627e92fed7f9fdbe4e65e0885e8698621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Tue, 28 May 2024 16:23:56 +0200 Subject: [PATCH 07/15] Update imports --- packages/admin/src/Models/Staff.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 0f8181fdf6..11144dada1 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -5,6 +5,8 @@ use Filament\Models\Contracts\FilamentUser; use Filament\Models\Contracts\HasName; use Filament\Panel; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; From baef4567d16e8c99d59c0c403c8cb71818b1cf9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Tue, 28 May 2024 16:25:24 +0200 Subject: [PATCH 08/15] Use snake case accessor for consistency --- packages/admin/src/Models/Staff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 11144dada1..63145a7f83 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -43,7 +43,7 @@ class Staff extends Authenticatable implements FilamentUser, HasName ]; protected $appends = [ - 'fullName', + 'full_name', ]; protected function fullName(): Attribute @@ -87,6 +87,6 @@ public function canAccessPanel(Panel $panel): bool public function getFilamentName(): string { - return $this->fullName; + return $this->full_name; } } From bfc90ec046fc44c655941cc557030a69f435a2c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Tue, 28 May 2024 16:26:39 +0200 Subject: [PATCH 09/15] Add class docblock for IDEs --- packages/admin/src/Models/Staff.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 63145a7f83..34b6270078 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -14,6 +14,22 @@ use Lunar\Admin\Database\Factories\StaffFactory; use Spatie\Permission\Traits\HasRoles; +/** + * @property int $id + * @property bool $admin + * @property string $firstname + * @property string $lastname + * @property string $full_name + * @property string $email + * @property string $password + * @property string $remember_token + * @property ?\Illuminate\Support\Carbon $email_verified_at + * @property ?\Illuminate\Support\Carbon $created_at + * @property ?\Illuminate\Support\Carbon $updated_at + * @property ?\Illuminate\Support\Carbon $deleted_at + * + * @method static \Illuminate\Database\Eloquent\Builder search(?string $terms) + */ class Staff extends Authenticatable implements FilamentUser, HasName { use HasFactory; From 77fbc52c5295b3a771f3a1bda25d53de967558e0 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Fri, 13 Sep 2024 10:38:29 +0100 Subject: [PATCH 10/15] Tests --- tests/admin/Unit/Models/StaffTest.php | 35 +++++++++++++++++++++++++++ tests/admin/Unit/Models/TestCase.php | 13 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/admin/Unit/Models/StaffTest.php create mode 100644 tests/admin/Unit/Models/TestCase.php diff --git a/tests/admin/Unit/Models/StaffTest.php b/tests/admin/Unit/Models/StaffTest.php new file mode 100644 index 0000000000..18999929a3 --- /dev/null +++ b/tests/admin/Unit/Models/StaffTest.php @@ -0,0 +1,35 @@ +group('lunar.admin.models'); + +test('can get full name', function () { + $staff = \Lunar\Admin\Models\Staff::factory()->create([ + 'firstname' => 'Joe', + 'lastname' => 'Bloggs', + ]); + + expect($staff->full_name)->toBe('Joe Bloggs'); +}); + +test('can search staff by name', function () { + \Lunar\Admin\Models\Staff::factory()->create([ + 'firstname' => 'Joe', + 'lastname' => 'Bloggs', + ]); + + \Lunar\Admin\Models\Staff::factory()->create([ + 'firstname' => 'Tim', + 'lastname' => 'Bloggs', + ]); + + \Lunar\Admin\Models\Staff::factory()->create([ + 'firstname' => 'Bill', + 'lastname' => 'Chance', + ]); + + expect(\Lunar\Admin\Models\Staff::search('Bloggs')->get())->toHaveCount(2) + ->and(\Lunar\Admin\Models\Staff::search('Bill')->get())->toHaveCount(1) + ->and(\Lunar\Admin\Models\Staff::search('Joe Bloggs')->get())->toHaveCount(1); +}); + diff --git a/tests/admin/Unit/Models/TestCase.php b/tests/admin/Unit/Models/TestCase.php new file mode 100644 index 0000000000..f765d78668 --- /dev/null +++ b/tests/admin/Unit/Models/TestCase.php @@ -0,0 +1,13 @@ + Date: Mon, 25 Nov 2024 08:42:19 +0000 Subject: [PATCH 11/15] chore: fix code style --- tests/admin/Unit/Models/StaffTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/admin/Unit/Models/StaffTest.php b/tests/admin/Unit/Models/StaffTest.php index 18999929a3..da356dba42 100644 --- a/tests/admin/Unit/Models/StaffTest.php +++ b/tests/admin/Unit/Models/StaffTest.php @@ -32,4 +32,3 @@ ->and(\Lunar\Admin\Models\Staff::search('Bill')->get())->toHaveCount(1) ->and(\Lunar\Admin\Models\Staff::search('Joe Bloggs')->get())->toHaveCount(1); }); - From da452eb155536fdb421b3372108abbc250912988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Mon, 25 Nov 2024 10:01:45 +0100 Subject: [PATCH 12/15] Update `firstname` `lastname` to have underscores --- packages/admin/README.md | 2 +- .../admin/database/factories/StaffFactory.php | 4 ++-- ...000_rename_name_columns_on_staff_table.php | 24 +++++++++++++++++++ packages/admin/resources/lang/en/staff.php | 8 +++---- packages/admin/resources/lang/es/staff.php | 8 +++---- packages/admin/resources/lang/fr/staff.php | 8 +++---- packages/admin/resources/lang/nl/staff.php | 8 +++---- .../Commands/MakeLunarAdminCommand.php | 4 ++-- .../src/Filament/Resources/StaffResource.php | 16 ++++++------- packages/admin/src/Models/Staff.php | 12 +++++----- .../StaffResource/Pages/CreateStaffTest.php | 4 ++-- .../StaffResource/Pages/EditStaffTest.php | 12 +++++----- tests/admin/Unit/Models/StaffTest.php | 17 +++++++------ 13 files changed, 75 insertions(+), 52 deletions(-) create mode 100644 packages/admin/database/migrations/2024_11_25_110000_rename_name_columns_on_staff_table.php diff --git a/packages/admin/README.md b/packages/admin/README.md index d48aaf62a3..6827a25ba9 100644 --- a/packages/admin/README.md +++ b/packages/admin/README.md @@ -13,7 +13,7 @@ See the docs at https://lunar-panel.vercel.app/ - [ ] Tax Zone types are "country", "states", "postcodes" -> would prefer it to be "countries" so it's consistent - [ ] `ListField` FieldType shouldn't really have "Field" in its name -> change to simply "List" - [ ] Sometimes we use "Enabled" other times "Active". Do we want to consider consistency here also? -- [ ] Staff "firstname" "lastname" I think should have underscores, e.g. "first_name" +- [x] Staff "firstname" "lastname" I think should have underscores, e.g. "first_name" - [x] How to allow devs to extend Lunar's forms and tables - [x] How to allow devs to overide validation rules - [x] Allow devs to extend by adding their own pages/resources diff --git a/packages/admin/database/factories/StaffFactory.php b/packages/admin/database/factories/StaffFactory.php index 0b50dfc9ea..3300af96ac 100644 --- a/packages/admin/database/factories/StaffFactory.php +++ b/packages/admin/database/factories/StaffFactory.php @@ -20,8 +20,8 @@ class StaffFactory extends Factory public function definition(): array { return [ - 'firstname' => $this->faker->firstName(), - 'lastname' => $this->faker->lastName(), + 'first_name' => $this->faker->firstName(), + 'last_name' => $this->faker->lastName(), 'admin' => $this->faker->boolean(5), 'email' => $this->faker->unique()->safeEmail(), 'email_verified_at' => now(), diff --git a/packages/admin/database/migrations/2024_11_25_110000_rename_name_columns_on_staff_table.php b/packages/admin/database/migrations/2024_11_25_110000_rename_name_columns_on_staff_table.php new file mode 100644 index 0000000000..17b1ab675b --- /dev/null +++ b/packages/admin/database/migrations/2024_11_25_110000_rename_name_columns_on_staff_table.php @@ -0,0 +1,24 @@ +prefix.'staff', function (Blueprint $table) { + $table->renameColumn('firstname', 'first_name'); + $table->renameColumn('lastname', 'last_name'); + }); + } + + public function down(): void + { + Schema::table($this->prefix.'staff', function (Blueprint $table) { + $table->renameColumn('first_name', 'firstname'); + $table->renameColumn('last_name', 'lastname'); + }); + } +}; diff --git a/packages/admin/resources/lang/en/staff.php b/packages/admin/resources/lang/en/staff.php index 5679833c40..d5b6e43a09 100644 --- a/packages/admin/resources/lang/en/staff.php +++ b/packages/admin/resources/lang/en/staff.php @@ -7,10 +7,10 @@ 'plural_label' => 'Staff', 'table' => [ - 'firstname' => [ + 'first_name' => [ 'label' => 'First Name', ], - 'lastname' => [ + 'last_name' => [ 'label' => 'Last Name', ], 'email' => [ @@ -22,10 +22,10 @@ ], 'form' => [ - 'firstname' => [ + 'first_name' => [ 'label' => 'First Name', ], - 'lastname' => [ + 'last_name' => [ 'label' => 'Last Name', ], 'email' => [ diff --git a/packages/admin/resources/lang/es/staff.php b/packages/admin/resources/lang/es/staff.php index bb244d3cc8..b1029457e7 100644 --- a/packages/admin/resources/lang/es/staff.php +++ b/packages/admin/resources/lang/es/staff.php @@ -7,10 +7,10 @@ 'plural_label' => 'Personal', 'table' => [ - 'firstname' => [ + 'first_name' => [ 'label' => 'Nombre', ], - 'lastname' => [ + 'last_name' => [ 'label' => 'Apellido', ], 'email' => [ @@ -22,10 +22,10 @@ ], 'form' => [ - 'firstname' => [ + 'first_name' => [ 'label' => 'Nombre', ], - 'lastname' => [ + 'last_name' => [ 'label' => 'Apellido', ], 'email' => [ diff --git a/packages/admin/resources/lang/fr/staff.php b/packages/admin/resources/lang/fr/staff.php index 14d8ae4feb..a163cba370 100644 --- a/packages/admin/resources/lang/fr/staff.php +++ b/packages/admin/resources/lang/fr/staff.php @@ -7,10 +7,10 @@ 'plural_label' => 'Personnel', 'table' => [ - 'firstname' => [ + 'first_name' => [ 'label' => 'Prénom', ], - 'lastname' => [ + 'last_name' => [ 'label' => 'Nom de famille', ], 'email' => [ @@ -22,10 +22,10 @@ ], 'form' => [ - 'firstname' => [ + 'first_name' => [ 'label' => 'Prénom', ], - 'lastname' => [ + 'last_name' => [ 'label' => 'Nom de famille', ], 'email' => [ diff --git a/packages/admin/resources/lang/nl/staff.php b/packages/admin/resources/lang/nl/staff.php index bf9add60b6..178a965d2f 100644 --- a/packages/admin/resources/lang/nl/staff.php +++ b/packages/admin/resources/lang/nl/staff.php @@ -7,10 +7,10 @@ 'plural_label' => 'Personeel', 'table' => [ - 'firstname' => [ + 'first_name' => [ 'label' => 'Voornaam', ], - 'lastname' => [ + 'last_name' => [ 'label' => 'Achternaam', ], 'email' => [ @@ -22,10 +22,10 @@ ], 'form' => [ - 'firstname' => [ + 'first_name' => [ 'label' => 'Voornaam', ], - 'lastname' => [ + 'last_name' => [ 'label' => 'Achternaam', ], 'email' => [ diff --git a/packages/admin/src/Console/Commands/MakeLunarAdminCommand.php b/packages/admin/src/Console/Commands/MakeLunarAdminCommand.php index 0b48d126fc..7e8bd2cba7 100644 --- a/packages/admin/src/Console/Commands/MakeLunarAdminCommand.php +++ b/packages/admin/src/Console/Commands/MakeLunarAdminCommand.php @@ -32,12 +32,12 @@ class MakeLunarAdminCommand extends Command protected function getUserData(): array { return [ - 'firstname' => $this->options['firstname'] ?? text( + 'first_name' => $this->options['firstname'] ?? text( label: 'First Name', required: true, ), - 'lastname' => $this->options['lastname'] ?? text( + 'last_name' => $this->options['lastname'] ?? text( label: 'Last Name', required: true, ), diff --git a/packages/admin/src/Filament/Resources/StaffResource.php b/packages/admin/src/Filament/Resources/StaffResource.php index f383eade58..019744072f 100644 --- a/packages/admin/src/Filament/Resources/StaffResource.php +++ b/packages/admin/src/Filament/Resources/StaffResource.php @@ -63,8 +63,8 @@ protected static function getMainFormComponents(): array protected static function getFirstNameFormComponent(): Component { - return Forms\Components\TextInput::make('firstname') - ->label(__('lunarpanel::staff.form.firstname.label')) + return Forms\Components\TextInput::make('first_name') + ->label(__('lunarpanel::staff.form.first_name.label')) ->required() ->maxLength(255) ->autofocus(); @@ -72,8 +72,8 @@ protected static function getFirstNameFormComponent(): Component protected static function getLastNameFormComponent(): Component { - return Forms\Components\TextInput::make('lastname') - ->label(__('lunarpanel::staff.form.lastname.label')) + return Forms\Components\TextInput::make('last_name') + ->label(__('lunarpanel::staff.form.last_name.label')) ->required() ->maxLength(255) ->autofocus(); @@ -169,10 +169,10 @@ public static function getDefaultTable(Table $table): Table { return $table ->columns([ - Tables\Columns\TextColumn::make('firstname') - ->label(__('lunarpanel::staff.table.firstname.label')), - Tables\Columns\TextColumn::make('lastname') - ->label(__('lunarpanel::staff.table.lastname.label')), + Tables\Columns\TextColumn::make('first_name') + ->label(__('lunarpanel::staff.table.first_name.label')), + Tables\Columns\TextColumn::make('last_name') + ->label(__('lunarpanel::staff.table.last_name.label')), Tables\Columns\TextColumn::make('email') ->label(__('lunarpanel::staff.table.email.label')), Tables\Columns\TextColumn::make('admin') diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 34b6270078..2feac6e4a4 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -17,8 +17,8 @@ /** * @property int $id * @property bool $admin - * @property string $firstname - * @property string $lastname + * @property string $first_name + * @property string $last_name * @property string $full_name * @property string $email * @property string $password @@ -40,8 +40,8 @@ class Staff extends Authenticatable implements FilamentUser, HasName protected $guard_name = 'staff'; protected $fillable = [ - 'firstname', - 'lastname', + 'first_name', + 'last_name', 'admin', 'email', 'password', @@ -65,7 +65,7 @@ class Staff extends Authenticatable implements FilamentUser, HasName protected function fullName(): Attribute { return Attribute::get( - fn (): string => "{$this->firstname} {$this->lastname}", + fn (): string => "{$this->first_name} {$this->last_name}", ); } @@ -92,7 +92,7 @@ public function scopeSearch(Builder $query, ?string $terms): void } foreach (explode(' ', $terms) as $term) { - $query->whereAny(['email', 'firstname', 'lastname'], 'LIKE', "%{$term}%"); + $query->whereAny(['email', 'first_name', 'last_name'], 'LIKE', "%{$term}%"); } } diff --git a/tests/admin/Feature/Filament/Resources/StaffResource/Pages/CreateStaffTest.php b/tests/admin/Feature/Filament/Resources/StaffResource/Pages/CreateStaffTest.php index 975a498e3e..838364731a 100644 --- a/tests/admin/Feature/Filament/Resources/StaffResource/Pages/CreateStaffTest.php +++ b/tests/admin/Feature/Filament/Resources/StaffResource/Pages/CreateStaffTest.php @@ -22,8 +22,8 @@ $staff->assignRole('staff'); $formData = [ - 'firstname' => $staff->firstname, - 'lastname' => $staff->lastname, + 'first_name' => $staff->first_name, + 'last_name' => $staff->last_name, 'email' => 'test@example.com', 'password' => 'password', ]; diff --git a/tests/admin/Feature/Filament/Resources/StaffResource/Pages/EditStaffTest.php b/tests/admin/Feature/Filament/Resources/StaffResource/Pages/EditStaffTest.php index 59c5c0df3d..d06e1623e2 100644 --- a/tests/admin/Feature/Filament/Resources/StaffResource/Pages/EditStaffTest.php +++ b/tests/admin/Feature/Filament/Resources/StaffResource/Pages/EditStaffTest.php @@ -24,8 +24,8 @@ 'record' => $staff->getRouteKey(), ]) ->assertFormSet([ - 'firstname' => $staff->firstname, - 'lastname' => $staff->lastname, + 'first_name' => $staff->first_name, + 'last_name' => $staff->last_name, 'email' => $staff->email, ]); }); @@ -39,16 +39,16 @@ 'record' => $staff->getRouteKey(), ]) ->fillForm([ - 'firstname' => $newData->firstname, - 'lastname' => $newData->lastname, + 'first_name' => $newData->first_name, + 'last_name' => $newData->last_name, 'email' => $newData->email, ]) ->call('save') ->assertHasNoFormErrors(); expect($staff->refresh()) - ->firstname->toBe($newData->firstname) - ->lastname->toBe($newData->lastname) + ->first_name->toBe($newData->first_name) + ->last_name->toBe($newData->last_name) ->email->toBe($newData->email); }); diff --git a/tests/admin/Unit/Models/StaffTest.php b/tests/admin/Unit/Models/StaffTest.php index 18999929a3..fa4ac00474 100644 --- a/tests/admin/Unit/Models/StaffTest.php +++ b/tests/admin/Unit/Models/StaffTest.php @@ -5,8 +5,8 @@ test('can get full name', function () { $staff = \Lunar\Admin\Models\Staff::factory()->create([ - 'firstname' => 'Joe', - 'lastname' => 'Bloggs', + 'first_name' => 'Joe', + 'last_name' => 'Bloggs', ]); expect($staff->full_name)->toBe('Joe Bloggs'); @@ -14,22 +14,21 @@ test('can search staff by name', function () { \Lunar\Admin\Models\Staff::factory()->create([ - 'firstname' => 'Joe', - 'lastname' => 'Bloggs', + 'first_name' => 'Joe', + 'last_name' => 'Bloggs', ]); \Lunar\Admin\Models\Staff::factory()->create([ - 'firstname' => 'Tim', - 'lastname' => 'Bloggs', + 'first_name' => 'Tim', + 'last_name' => 'Bloggs', ]); \Lunar\Admin\Models\Staff::factory()->create([ - 'firstname' => 'Bill', - 'lastname' => 'Chance', + 'first_name' => 'Bill', + 'last_name' => 'Chance', ]); expect(\Lunar\Admin\Models\Staff::search('Bloggs')->get())->toHaveCount(2) ->and(\Lunar\Admin\Models\Staff::search('Bill')->get())->toHaveCount(1) ->and(\Lunar\Admin\Models\Staff::search('Joe Bloggs')->get())->toHaveCount(1); }); - From 789983946eb3fdcec9167654ea6c782889f36993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Mon, 25 Nov 2024 10:05:34 +0100 Subject: [PATCH 13/15] Fix migrations for SQLite "SQLite doesn't support multiple calls to dropColumn / renameColumn in a single modification." --- ...ename_firstname_column_on_staff_table.php} | 2 -- ..._rename_lastname_column_on_staff_table.php | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) rename packages/admin/database/migrations/{2024_11_25_110000_rename_name_columns_on_staff_table.php => 2024_11_25_110000_rename_firstname_column_on_staff_table.php} (82%) create mode 100644 packages/admin/database/migrations/2024_11_25_110001_rename_lastname_column_on_staff_table.php diff --git a/packages/admin/database/migrations/2024_11_25_110000_rename_name_columns_on_staff_table.php b/packages/admin/database/migrations/2024_11_25_110000_rename_firstname_column_on_staff_table.php similarity index 82% rename from packages/admin/database/migrations/2024_11_25_110000_rename_name_columns_on_staff_table.php rename to packages/admin/database/migrations/2024_11_25_110000_rename_firstname_column_on_staff_table.php index 17b1ab675b..357cf0c28d 100644 --- a/packages/admin/database/migrations/2024_11_25_110000_rename_name_columns_on_staff_table.php +++ b/packages/admin/database/migrations/2024_11_25_110000_rename_firstname_column_on_staff_table.php @@ -10,7 +10,6 @@ public function up(): void { Schema::table($this->prefix.'staff', function (Blueprint $table) { $table->renameColumn('firstname', 'first_name'); - $table->renameColumn('lastname', 'last_name'); }); } @@ -18,7 +17,6 @@ public function down(): void { Schema::table($this->prefix.'staff', function (Blueprint $table) { $table->renameColumn('first_name', 'firstname'); - $table->renameColumn('last_name', 'lastname'); }); } }; diff --git a/packages/admin/database/migrations/2024_11_25_110001_rename_lastname_column_on_staff_table.php b/packages/admin/database/migrations/2024_11_25_110001_rename_lastname_column_on_staff_table.php new file mode 100644 index 0000000000..98be1ed52e --- /dev/null +++ b/packages/admin/database/migrations/2024_11_25_110001_rename_lastname_column_on_staff_table.php @@ -0,0 +1,22 @@ +prefix.'staff', function (Blueprint $table) { + $table->renameColumn('lastname', 'last_name'); + }); + } + + public function down(): void + { + Schema::table($this->prefix.'staff', function (Blueprint $table) { + $table->renameColumn('last_name', 'lastname'); + }); + } +}; From af97cf2224a7049a065dfa7341c2b119433d68c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Mon, 25 Nov 2024 10:37:31 +0100 Subject: [PATCH 14/15] Add fallbacks for old keys --- packages/admin/src/Models/Staff.php | 16 +++++++++++ tests/admin/Unit/Models/StaffTest.php | 38 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 2feac6e4a4..8e75f58bf2 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -62,6 +62,22 @@ class Staff extends Authenticatable implements FilamentUser, HasName 'full_name', ]; + protected function firstName(): Attribute + { + return Attribute::make( + get: fn (): string => $this->first_name, + set: fn (?string $value) => $this->first_name = $value, + ); + } + + protected function lastName(): Attribute + { + return Attribute::make( + get: fn (): string => $this->last_name, + set: fn (?string $value) => $this->last_name = $value, + ); + } + protected function fullName(): Attribute { return Attribute::get( diff --git a/tests/admin/Unit/Models/StaffTest.php b/tests/admin/Unit/Models/StaffTest.php index fa4ac00474..bdaad06067 100644 --- a/tests/admin/Unit/Models/StaffTest.php +++ b/tests/admin/Unit/Models/StaffTest.php @@ -32,3 +32,41 @@ ->and(\Lunar\Admin\Models\Staff::search('Bill')->get())->toHaveCount(1) ->and(\Lunar\Admin\Models\Staff::search('Joe Bloggs')->get())->toHaveCount(1); }); + +test('can get first name by old key without underscore', function () { + $staff = \Lunar\Admin\Models\Staff::factory()->create([ + 'first_name' => 'Joe', + ]); + + expect($staff->firstname)->toBe('Joe'); +}); + +test('can get last name by old key without underscore', function () { + $staff = \Lunar\Admin\Models\Staff::factory()->create([ + 'last_name' => 'Bloggs', + ]); + + expect($staff->lastname)->toBe('Bloggs'); +}); + +test('can set first name by old key without underscore', function () { + $staff = \Lunar\Admin\Models\Staff::factory()->create([ + 'first_name' => 'Joe', + ]); + + $staff->firstname = 'Tim'; + + expect($staff->firstname)->toBe('Tim'); + expect($staff->first_name)->toBe('Tim'); +}); + +test('can set last name by old key without underscore', function () { + $staff = \Lunar\Admin\Models\Staff::factory()->create([ + 'last_name' => 'Bloggs', + ]); + + $staff->lastname = 'Chance'; + + expect($staff->lastname)->toBe('Chance'); + expect($staff->last_name)->toBe('Chance'); +}); From 860be2c52b63b7f3e6abf70bddaa55e5f5517b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Mon, 25 Nov 2024 11:00:48 +0100 Subject: [PATCH 15/15] Fix fallbacks to actually work... --- packages/admin/src/Models/Staff.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/admin/src/Models/Staff.php b/packages/admin/src/Models/Staff.php index 8e75f58bf2..28689d5170 100644 --- a/packages/admin/src/Models/Staff.php +++ b/packages/admin/src/Models/Staff.php @@ -62,19 +62,19 @@ class Staff extends Authenticatable implements FilamentUser, HasName 'full_name', ]; - protected function firstName(): Attribute + protected function firstname(): Attribute { return Attribute::make( - get: fn (): string => $this->first_name, - set: fn (?string $value) => $this->first_name = $value, + get: fn (mixed $value, array $attributes) => $attributes['first_name'], + set: fn (string $value) => ['first_name' => $value], ); } - protected function lastName(): Attribute + protected function lastname(): Attribute { return Attribute::make( - get: fn (): string => $this->last_name, - set: fn (?string $value) => $this->last_name = $value, + get: fn (mixed $value, array $attributes) => $attributes['last_name'], + set: fn (string $value) => ['last_name' => $value], ); }