Browse Source

add employees table

3.x
Andrew Wallo 2 years ago
parent
commit
8701ebb508

+ 1
- 1
app/Filament/Pages/Companies.php View File

24
     protected function getHeaderWidgets(): array
24
     protected function getHeaderWidgets(): array
25
     {
25
     {
26
         return [
26
         return [
27
-            \App\Filament\Pages\Widgets\Companies::class,
27
+            Widgets\Companies::class,
28
         ];
28
         ];
29
     }
29
     }
30
 }
30
 }

+ 30
- 0
app/Filament/Pages/Employees.php View File

1
+<?php
2
+
3
+namespace App\Filament\Pages;
4
+
5
+use Filament\Pages\Page;
6
+use Illuminate\Support\Facades\Auth;
7
+
8
+class Employees extends Page
9
+{
10
+    protected static ?string $navigationIcon = 'heroicon-o-users';
11
+
12
+    protected static string $view = 'filament.pages.employees';
13
+
14
+    protected static function shouldRegisterNavigation(): bool
15
+    {
16
+        return Auth::user()->currentCompany->name === 'ERPSAAS';
17
+    }
18
+
19
+    public function mount(): void
20
+    {
21
+        abort_unless(Auth::user()->currentCompany->name === 'ERPSAAS', 403);
22
+    }
23
+
24
+    protected function getHeaderWidgets(): array
25
+    {
26
+        return [
27
+            Widgets\Employees::class,
28
+        ];
29
+    }
30
+}

+ 1
- 1
app/Filament/Pages/Users.php View File

24
     protected function getHeaderWidgets(): array
24
     protected function getHeaderWidgets(): array
25
     {
25
     {
26
         return [
26
         return [
27
-            \App\Filament\Pages\Widgets\Users::class,
27
+            Widgets\Users::class,
28
         ];
28
         ];
29
     }
29
     }
30
 }
30
 }

+ 3
- 1
app/Filament/Pages/Widgets/Companies.php View File

49
                 ->view('filament.components.companies.avatar-column')
49
                 ->view('filament.components.companies.avatar-column')
50
                 ->label('Owner')
50
                 ->label('Owner')
51
                 ->sortable()
51
                 ->sortable()
52
+                ->searchable()
52
                 ->grow(false),
53
                 ->grow(false),
53
             Tables\Columns\TextColumn::make('name')
54
             Tables\Columns\TextColumn::make('name')
54
                 ->weight('semibold')
55
                 ->weight('semibold')
55
                 ->label('Company')
56
                 ->label('Company')
56
-                ->sortable(),
57
+                ->sortable()
58
+                ->searchable(),
57
             Tables\Columns\TextColumn::make('users_count')
59
             Tables\Columns\TextColumn::make('users_count')
58
                 ->label('Employees')
60
                 ->label('Employees')
59
                 ->weight('semibold')
61
                 ->weight('semibold')

+ 70
- 0
app/Filament/Pages/Widgets/Employees.php View File

1
+<?php
2
+
3
+namespace App\Filament\Pages\Widgets;
4
+
5
+use App\Models\User;
6
+use Closure;
7
+use Exception;
8
+use Filament\Tables;
9
+use Filament\Widgets\TableWidget as PageWidget;
10
+use Illuminate\Contracts\Support\Htmlable;
11
+use Illuminate\Database\Eloquent\Builder;
12
+use Illuminate\Database\Eloquent\Relations\Relation;
13
+
14
+class Employees extends PageWidget
15
+{
16
+    protected int|string|array $columnSpan = [
17
+        'md' => 2,
18
+        'xl' => 3,
19
+    ];
20
+
21
+    protected function getTableQuery(): Builder|Relation
22
+    {
23
+        return User::whereHas('employeeships');
24
+    }
25
+
26
+    protected function getTableHeading(): string|Htmlable|Closure|null
27
+    {
28
+        return null;
29
+    }
30
+
31
+    /**
32
+     * @throws Exception
33
+     */
34
+    protected function getTableFilters(): array
35
+    {
36
+        return [
37
+            Tables\Filters\SelectFilter::make('name')
38
+                ->label('Company')
39
+                ->relationship('companies', 'name', static fn (Builder $query) => $query->whereHas('users')),
40
+        ];
41
+    }
42
+
43
+    protected function getTableColumns(): array
44
+    {
45
+        return [
46
+            Tables\Columns\ViewColumn::make('name')
47
+                ->view('filament.components.users.avatar-column')
48
+                ->label('Name')
49
+                ->sortable()
50
+                ->searchable()
51
+                ->grow(false),
52
+            Tables\Columns\TextColumn::make('companies.name')
53
+                ->label('Company')
54
+                ->sortable()
55
+                ->searchable()
56
+                ->weight('semibold'),
57
+            Tables\Columns\BadgeColumn::make('employeeships.role')
58
+                ->label('Role')
59
+                ->icons([
60
+                    'heroicon-o-shield-check' => 'admin',
61
+                    'heroicon-o-pencil' => 'editor',
62
+                ])
63
+                ->colors([
64
+                    'primary' => 'admin',
65
+                    'warning' => 'editor',
66
+                ])
67
+                ->sortable(),
68
+        ];
69
+    }
70
+}

+ 1
- 0
app/Filament/Pages/Widgets/Users.php View File

34
                 ->view('filament.components.users.avatar-column')
34
                 ->view('filament.components.users.avatar-column')
35
                 ->label('Name')
35
                 ->label('Name')
36
                 ->sortable()
36
                 ->sortable()
37
+                ->searchable()
37
                 ->grow(false),
38
                 ->grow(false),
38
             Tables\Columns\TextColumn::make('owned_companies')
39
             Tables\Columns\TextColumn::make('owned_companies')
39
                 ->label('Companies')
40
                 ->label('Companies')

+ 6
- 0
app/Models/User.php View File

5
 use Filament\Models\Contracts\FilamentUser;
5
 use Filament\Models\Contracts\FilamentUser;
6
 use Filament\Models\Contracts\HasAvatar;
6
 use Filament\Models\Contracts\HasAvatar;
7
 use Illuminate\Database\Eloquent\Factories\HasFactory;
7
 use Illuminate\Database\Eloquent\Factories\HasFactory;
8
+use Illuminate\Database\Eloquent\Relations\HasMany;
8
 use Illuminate\Foundation\Auth\User as Authenticatable;
9
 use Illuminate\Foundation\Auth\User as Authenticatable;
9
 use Illuminate\Notifications\Notifiable;
10
 use Illuminate\Notifications\Notifiable;
10
 use Laravel\Fortify\TwoFactorAuthenticatable;
11
 use Laravel\Fortify\TwoFactorAuthenticatable;
35
         return $this->profile_photo_url;
36
         return $this->profile_photo_url;
36
     }
37
     }
37
 
38
 
39
+    public function employeeships(): HasMany
40
+    {
41
+        return $this->hasMany(Employeeship::class);
42
+    }
43
+
38
     /**
44
     /**
39
      * The attributes that are mass assignable.
45
      * The attributes that are mass assignable.
40
      *
46
      *

+ 3
- 0
resources/views/filament/pages/employees.blade.php View File

1
+<x-filament::page>
2
+
3
+</x-filament::page>

Loading…
Cancel
Save