Skip to content

Commit 5b90809

Browse files
authored
feat: add more user model attributes
1 parent b05d2d9 commit 5b90809

4 files changed

Lines changed: 100 additions & 1 deletion

File tree

database/factories/UserFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Eclipse\Core\Database\Factories;
44

55
use Eclipse\Core\Models\User;
6+
use Eclipse\World\Models\Country;
67
use Illuminate\Database\Eloquent\Factories\Factory;
78
use Illuminate\Support\Facades\Hash;
89
use Illuminate\Support\Str;
@@ -35,8 +36,11 @@ public function definition(): array
3536
'first_name' => fake()->firstName(),
3637
'last_name' => fake()->lastName(),
3738
'email' => fake()->unique()->safeEmail(),
39+
'phone_number' => fake()->phoneNumber(),
3840
'email_verified_at' => now(),
3941
'password' => static::$password ??= Hash::make('password'),
42+
'country_id' => Country::inRandomOrder()->first()?->id ?? Country::factory()->create()->id,
43+
'date_of_birth' => now()->subYears(rand(20, 40)),
4044
'remember_token' => Str::random(10),
4145
'login_count' => 0,
4246
];
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table('users', function (Blueprint $table) {
12+
$table->string('phone_number', 20)
13+
->after('email')
14+
->nullable();
15+
$table->string('country_id', 2)
16+
->after('password')
17+
->nullable();
18+
$table->date('date_of_birth')
19+
->after('country_id')
20+
->nullable();
21+
22+
$table->index('phone_number');
23+
$table->index('country_id');
24+
$table->index('date_of_birth');
25+
26+
$table->foreign('country_id')
27+
->references('id')
28+
->on('world_countries')
29+
->onDelete('set null');
30+
});
31+
}
32+
33+
public function down()
34+
{
35+
Schema::table('users', function (Blueprint $table) {
36+
$table->dropForeign(['country_id']);
37+
38+
$table->dropIndex(['phone_number']);
39+
$table->dropIndex(['country_id']);
40+
$table->dropIndex(['date_of_birth']);
41+
42+
$table->dropColumn([
43+
'date_of_birth',
44+
'phone_number',
45+
'country_id',
46+
]);
47+
});
48+
}
49+
};

src/Filament/Resources/UserResource.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public static function form(Form $form): Form
5050
self::getFirstNameFormComponent(),
5151
self::getLastNameFormComponent(),
5252
self::getEmailFormComponent(),
53+
Forms\Components\TextInput::make('phone_number')
54+
->label('Phone')
55+
->tel(),
5356
Forms\Components\DateTimePicker::make('email_verified_at')
5457
->visible(config('eclipse.email_verification'))
5558
->disabled(),
@@ -69,6 +72,15 @@ public static function form(Form $form): Form
6972
fn (Set $set) => $set('password', Str::password(16))
7073
)
7174
),
75+
Forms\Components\Select::make('country_id')
76+
->relationship('country', 'name')
77+
->preload()
78+
->optionsLimit(20)
79+
->searchable(),
80+
Forms\Components\DatePicker::make('date_of_birth')
81+
->native(false)
82+
->minDate(now()->subYears(80))
83+
->maxDate(now()),
7284
Forms\Components\Select::make('roles')
7385
->relationship('roles', 'name')
7486
->saveRelationshipsUsing(function (User $record, $state) {
@@ -129,6 +141,9 @@ public static function table(Table $table): Table
129141
->width(150);
130142
}
131143

144+
$columns[] = Tables\Columns\TextColumn::make('phone_number')
145+
->label('Phone');
146+
132147
$columns[] = Tables\Columns\TextColumn::make('email_verified_at')
133148
->label('Verified email')
134149
->placeholder('Not verified')
@@ -138,6 +153,12 @@ public static function table(Table $table): Table
138153
->visible(config('eclipse.email_verification'))
139154
->width(150);
140155

156+
$columns[] = Tables\Columns\TextColumn::make('country.name')
157+
->badge();
158+
159+
$columns[] = Tables\Columns\TextColumn::make('date_of_birth')
160+
->date('M d, Y');
161+
141162
$columns[] = Tables\Columns\TextColumn::make('created_at')
142163
->dateTime()
143164
->sortable()
@@ -163,6 +184,12 @@ public static function table(Table $table): Table
163184
blank: fn (Builder $query) => $query,
164185
)
165186
->visible(config('eclipse.email_verification')),
187+
Tables\Filters\SelectFilter::make('country_id')
188+
->label('Country')
189+
->multiple()
190+
->relationship('country', 'name', fn (Builder $query): Builder => $query->distinct())
191+
->preload()
192+
->optionsLimit(20),
166193
Tables\Filters\QueryBuilder::make()
167194
->constraints([
168195
TextConstraint::make('first_name')
@@ -231,7 +258,7 @@ public static function infolist(Infolist $infolist): Infolist
231258
->dateTime(),
232259
]),
233260
Section::make('Personal information')
234-
->columns(3)
261+
->columns(4)
235262
->schema([
236263
SpatieMediaLibraryImageEntry::make('avatar')
237264
->collection('avatars')
@@ -245,6 +272,14 @@ public static function infolist(Infolist $infolist): Infolist
245272
->icon(config('eclipse.email_verification') ? fn (User $user) => $user->email_verified_at ? 'heroicon-s-check-circle' : 'heroicon-s-x-circle' : null)
246273
->iconColor(fn (User $user) => $user->email_verified_at ? Color::Green : Color::Red),
247274
]),
275+
Group::make()
276+
->schema([
277+
TextEntry::make('phone_number')->placeholder('-'),
278+
TextEntry::make('country.name')
279+
->badge()
280+
->placeholder('-'),
281+
]),
282+
TextEntry::make('date_of_birth')->date('M d, Y')->placeholder('-'),
248283
]),
249284
]);
250285
}

src/Models/User.php

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

55
use Eclipse\Core\Database\Factories\UserFactory;
6+
use Eclipse\World\Models\Country;
67
use Eclipse\Core\Settings\UserSettings;
78
use Exception;
89
use Filament\Models\Contracts\FilamentUser;
@@ -11,6 +12,7 @@
1112
use Filament\Panel;
1213
use Illuminate\Database\Eloquent\Factories\HasFactory;
1314
use Illuminate\Database\Eloquent\Model;
15+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1416
use Illuminate\Database\Eloquent\SoftDeletes;
1517
use Illuminate\Foundation\Auth\User as Authenticatable;
1618
use Illuminate\Notifications\Notifiable;
@@ -50,7 +52,10 @@ class User extends Authenticatable implements FilamentUser, HasAvatar, HasMedia,
5052
'first_name',
5153
'last_name',
5254
'email',
55+
'phone_number',
5356
'password',
57+
'country_id',
58+
'date_of_birth',
5459
'last_login_at',
5560
'login_count',
5661
];
@@ -75,6 +80,7 @@ protected function casts(): array
7580
return [
7681
'email_verified_at' => 'datetime',
7782
'password' => 'hashed',
83+
'date_of_birth' => 'date',
7884
'last_login_at' => 'datetime',
7985
];
8086
}
@@ -89,6 +95,11 @@ public function sites()
8995
return $this->belongsToMany(Site::class, 'site_has_user');
9096
}
9197

98+
public function country(): BelongsTo
99+
{
100+
return $this->belongsTo(Country::class);
101+
}
102+
92103
public function getFilamentAvatarUrl(): ?string
93104
{
94105
return $this->getMedia('avatars')->first()?->getUrl();

0 commit comments

Comments
 (0)