22
33namespace App \Http \Livewire \Administration ;
44
5- use App \Models \Company ;
65use App \Models \User ;
76use App \Notifications \UserCreatedNotification ;
8- use Filament \ Forms \ Components \ TagsInput ;
7+ use Carbon \ Carbon ;
98use Filament \Notifications \Notification ;
109use Filament \Tables \Actions \Action ;
11- use Filament \Tables \Columns \BadgeColumn ;
1210use Filament \Tables \Columns \BooleanColumn ;
1311use Filament \Tables \Columns \TagsColumn ;
1412use Filament \Tables \Columns \TextColumn ;
1816use Illuminate \Database \Eloquent \Builder ;
1917use Illuminate \Database \Eloquent \Relations \Relation ;
2018use Livewire \Component ;
19+ use pxlrbt \FilamentExcel \Actions \Tables \ExportAction ;
20+ use pxlrbt \FilamentExcel \Columns \Column ;
21+ use pxlrbt \FilamentExcel \Exports \ExcelExport ;
2122
2223class Users extends Component implements HasTable
2324{
@@ -43,11 +44,10 @@ protected function getTableQuery(): Builder|Relation
4344 if (auth ()->user ()->can ('View company users ' ) && !auth ()->user ()->can ('View all users ' )) {
4445 $ query ->whereHas (
4546 'companies ' ,
46- fn ($ query ) =>
47- $ query ->whereIn (
48- 'companies.id ' ,
49- auth ()->user ()->ownCompanies ->pluck ('id ' )->toArray ()
50- )
47+ fn ($ query ) => $ query ->whereIn (
48+ 'companies.id ' ,
49+ auth ()->user ()->ownCompanies ->pluck ('id ' )->toArray ()
50+ )
5151 );
5252 } elseif (!auth ()->user ()->can ('View all users ' )) {
5353 // Get empty list
@@ -75,7 +75,7 @@ protected function getTableColumns(): array
7575 TagsColumn::make ('roles.name ' )
7676 ->label (__ ('User roles ' ))
7777 ->limit (1 )
78- ->visible (fn () => auth ()->user ()->can ('Assign permissions ' ))
78+ ->visible (fn () => auth ()->user ()->can ('Assign permissions ' ))
7979 ->searchable ()
8080 ->sortable (),
8181
@@ -113,11 +113,48 @@ protected function getTableActions(): array
113113 ->icon ('heroicon-o-pencil ' )
114114 ->link ()
115115 ->label (__ ('Edit user ' ))
116- ->visible (fn () => auth ()->user ()->can ('Update users ' ))
116+ ->visible (fn () => auth ()->user ()->can ('Update users ' ))
117117 ->action (fn (User $ record ) => $ this ->updateUser ($ record ->id ))
118118 ];
119119 }
120120
121+ /**
122+ * Table header actions definition
123+ *
124+ * @return array
125+ */
126+ protected function getTableHeaderActions (): array
127+ {
128+ return [
129+ ExportAction::make ()
130+ ->label (__ ('Export ' ))
131+ ->color ('success ' )
132+ ->icon ('heroicon-o-document-download ' )
133+ ->exports ([
134+ ExcelExport::make ()
135+ ->askForWriterType ()
136+ ->withFilename ('users-export ' )
137+ ->withColumns ([
138+ Column::make ('name ' )
139+ ->heading (__ ('Full name ' )),
140+ Column::make ('companies ' )
141+ ->heading (__ ('Companies ' ))
142+ ->formatStateUsing (
143+ fn (User $ record ) => $ record ->companies ->pluck ('name ' )->join (', ' )
144+ ),
145+ Column::make ('roles ' )
146+ ->heading (__ ('User roles ' ))
147+ ->formatStateUsing (
148+ fn (User $ record ) => $ record ->roles ->pluck ('name ' )->join (', ' )
149+ ),
150+ Column::make ('created_at ' )
151+ ->heading (__ ('Created at ' ))
152+ ->formatStateUsing (fn (Carbon $ state ) => $ state ->format (__ ('Y-m-d g:i A ' ))),
153+ ])
154+ ])
155+ ];
156+ }
157+
121158 /**
122159 * Table default sort column definition
123160 *
0 commit comments