Skip to content

Commit fc5c423

Browse files
authored
✨ Add & Setup Filament Admin (#129)
* ✨ Add & Setup Filament Admin * Fix code styling --------- Co-authored-by: mckenziearts <[email protected]>
1 parent 48ce6fa commit fc5c423

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+547
-139
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ APP_ENV=local
33
APP_KEY=
44
APP_DEBUG=true
55
APP_URL=http://laravel.cm.test
6-
FRONTEND_APP_URL=http://localhost::4200
6+
FILAMENT_PATH=cp
7+
FRONTEND_APP_URL=http://localhost:4200
78

89
LOG_CHANNEL=stack
910
LOG_LEVEL=debug

app/Models/User.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use App\Traits\HasSettings;
1010
use App\Traits\HasUsername;
1111
use App\Traits\Reacts;
12+
use Filament\Models\Contracts\FilamentUser;
13+
use Filament\Models\Contracts\HasAvatar;
14+
use Filament\Models\Contracts\HasName;
1215
use Illuminate\Contracts\Auth\MustVerifyEmail;
1316
use Illuminate\Database\Eloquent\Builder;
1417
use Illuminate\Database\Eloquent\Collection;
@@ -32,7 +35,7 @@
3235
/**
3336
* @mixin IdeHelperUser
3437
*/
35-
class User extends Authenticatable implements MustVerifyEmail, HasMedia, FeaturableInterface
38+
class User extends Authenticatable implements MustVerifyEmail, HasMedia, FeaturableInterface, FilamentUser, HasName, HasAvatar
3639
{
3740
use Gamify;
3841
use HasFactory;
@@ -47,11 +50,6 @@ class User extends Authenticatable implements MustVerifyEmail, HasMedia, Featura
4750
use Reacts;
4851
use Featurable;
4952

50-
/**
51-
* The attributes that are mass assignable.
52-
*
53-
* @var array<string>
54-
*/
5553
protected $fillable = [
5654
'name',
5755
'email',
@@ -74,34 +72,19 @@ class User extends Authenticatable implements MustVerifyEmail, HasMedia, Featura
7472
'opt_in',
7573
];
7674

77-
/**
78-
* The attributes that should be hidden for arrays.
79-
*
80-
* @var array<int, string>
81-
*/
8275
protected $hidden = [
8376
'password',
8477
'remember_token',
8578
'two_factor_recovery_codes',
8679
'two_factor_secret',
8780
];
8881

89-
/**
90-
* The attributes that should be cast to native types.
91-
*
92-
* @var array<string, string>
93-
*/
9482
protected $casts = [
9583
'email_verified_at' => 'datetime',
9684
'last_login_at' => 'datetime',
9785
'settings' => 'array',
9886
];
9987

100-
/**
101-
* The accessors to append to the model's array form.
102-
*
103-
* @var string[]
104-
*/
10588
protected $appends = [
10689
'profile_photo_url',
10790
'roles_label',
@@ -179,6 +162,21 @@ public function isLoggedInUser(): bool
179162
return $this->id === Auth::id();
180163
}
181164

165+
public function canAccessFilament(): bool
166+
{
167+
return str_ends_with($this->email, '@laravel.cm') || $this->isModerator() || $this->isAdmin();
168+
}
169+
170+
public function getFilamentName(): string
171+
{
172+
return $this->name;
173+
}
174+
175+
public function getFilamentAvatarUrl(): ?string
176+
{
177+
return $this->profile_photo_url;
178+
}
179+
182180
/**
183181
* @return array{name: string, username: string, picture: string}
184182
*/

app/Providers/AppServiceProvider.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use App\View\Composers\TopContributorsComposer;
1919
use App\View\Composers\TopMembersComposer;
2020
use Carbon\Carbon;
21+
use Filament\Facades\Filament;
2122
use Illuminate\Database\Eloquent\Relations\Relation;
2223
use Illuminate\Support\Facades\Blade;
2324
use Illuminate\Support\Facades\View;
@@ -41,23 +42,16 @@ public function boot(): void
4142
$this->bootMacros();
4243
$this->bootViewsComposer();
4344
$this->bootEloquentMorphs();
45+
$this->bootFilament();
4446

4547
ReplyResource::withoutWrapping();
4648
}
4749

4850
public function registerBladeDirective(): void
4951
{
50-
Blade::directive('title', function ($expression) {
51-
return "<?php \$title = $expression ?>";
52-
});
53-
54-
Blade::directive('shareImage', function ($expression) {
55-
return "<?php \$shareImage = $expression ?>";
56-
});
57-
58-
Blade::directive('canonical', function ($expression) {
59-
return "<?php \$canonical = $expression ?>";
60-
});
52+
Blade::directive('title', fn ($expression) => "<?php \$title = $expression ?>");
53+
Blade::directive('shareImage', fn ($expression) => "<?php \$shareImage = $expression ?>");
54+
Blade::directive('canonical', fn ($expression) => "<?php \$canonical = $expression ?>");
6155
}
6256

6357
public function bootMacros(): void
@@ -91,4 +85,18 @@ public function bootEloquentMorphs(): void
9185
'user' => User::class,
9286
]);
9387
}
88+
89+
public function bootFilament(): void
90+
{
91+
Filament::serving(function () {
92+
Filament::registerTheme(
93+
mix('css/filament.css'),
94+
);
95+
});
96+
97+
Filament::registerRenderHook(
98+
'body.start',
99+
fn (): string => Blade::render('@livewire(\'livewire-ui-modal\')'),
100+
);
101+
}
94102
}

app/Providers/RouteServiceProvider.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Providers;
66

7+
use App\Models\User;
78
use Illuminate\Cache\RateLimiting\Limit;
89
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
910
use Illuminate\Http\Request;
@@ -12,13 +13,6 @@
1213

1314
final class RouteServiceProvider extends ServiceProvider
1415
{
15-
/**
16-
* The path to the "home" route for your application.
17-
*
18-
* This is used by Laravel authentication to redirect users after login.
19-
*
20-
* @var string
21-
*/
2216
public const HOME = '/dashboard';
2317

2418
public function boot(): void
@@ -36,37 +30,31 @@ public function boot(): void
3630
Route::middleware('web')
3731
->namespace($this->namespace)
3832
->group(base_path('routes/web.php'));
39-
40-
Route::middleware(['web', 'auth', 'role:moderator|admin'])
41-
->namespace($this->namespace)
42-
->prefix('cpanel')
43-
->as('cpanel.')
44-
->group(base_path('routes/cpanel.php'));
4533
});
4634

47-
Route::macro('redirectMap', function ($map, $status = 302) {
35+
Route::macro('redirectMap', function (array $map, int $status = 302) {
4836
foreach ($map as $old => $new) {
4937
Route::redirect($old, $new, $status)->name($old);
5038
}
5139
});
5240
}
5341

54-
/**
55-
* Configure the rate limiters for the application.
56-
*
57-
* @return void
58-
*/
5942
protected function configureRateLimiting(): void
6043
{
61-
RateLimiter::for('api', function (Request $request) {
62-
return Limit::perMinute(60)->by((string) (optional($request->user())->id ?: $request->ip()));
63-
});
44+
RateLimiter::for(
45+
name: 'api',
46+
callback: fn (Request $request): Limit => Limit::perMinute(60)
47+
->by(
48+
(string) (optional($request->user())->id ?: $request->ip())
49+
)
50+
);
6451
}
6552

6653
protected function routeBindings(): void
6754
{
68-
Route::bind('username', function (string $username) {
69-
return \App\Models\User::findByUsername($username);
70-
});
55+
Route::bind(
56+
key: 'username',
57+
binder: fn (string $username): User => User::findByUsername($username)
58+
);
7159
}
7260
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"blade-ui-kit/blade-ui-kit": "^0.3.4",
1616
"cyrildewit/eloquent-viewable": "^6.1",
1717
"doctrine/dbal": "^3.6.4",
18+
"filament/filament": "^2.0",
1819
"filament/notifications": "^2.17.49",
1920
"francescomalatesta/laravel-feature": "^3.0",
2021
"graham-campbell/markdown": "^14.0",

0 commit comments

Comments
 (0)