Added dynamic plant options in user form based on authenticated user's plant ID
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
This commit is contained in:
@@ -5,7 +5,7 @@ namespace App\Filament\Resources;
|
|||||||
use App\Filament\Exports\UserExporter;
|
use App\Filament\Exports\UserExporter;
|
||||||
use App\Filament\Imports\UserImporter;
|
use App\Filament\Imports\UserImporter;
|
||||||
use App\Filament\Resources\UserResource\Pages;
|
use App\Filament\Resources\UserResource\Pages;
|
||||||
use App\Filament\Resources\UserResource\RelationManagers;
|
use App\Models\Plant;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Filament\Facades\Filament;
|
use Filament\Facades\Filament;
|
||||||
use Filament\Forms;
|
use Filament\Forms;
|
||||||
@@ -36,16 +36,21 @@ class UserResource extends Resource
|
|||||||
->relationship('plant', 'name')
|
->relationship('plant', 'name')
|
||||||
->nullable()
|
->nullable()
|
||||||
->reactive()
|
->reactive()
|
||||||
|
->options(function (callable $get) {
|
||||||
|
$userHas = Filament::auth()->user()->plant_id;
|
||||||
|
|
||||||
|
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
|
||||||
|
})
|
||||||
->default(function () {
|
->default(function () {
|
||||||
return optional(User::latest()->first())->plant_id;
|
return optional(User::latest()->first())->plant_id;
|
||||||
}),
|
}),
|
||||||
Forms\Components\TextInput::make('name')
|
Forms\Components\TextInput::make('name')
|
||||||
->required()
|
->required()
|
||||||
->autofocus()
|
->autofocus()
|
||||||
//->reactive()
|
// ->reactive()
|
||||||
->live(debounce: 600)
|
->live(debounce: 600)
|
||||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
$set('email', $state . '@cripumps.com');
|
$set('email', $state.'@cripumps.com');
|
||||||
})
|
})
|
||||||
->maxLength(255),
|
->maxLength(255),
|
||||||
Forms\Components\TextInput::make('email')
|
Forms\Components\TextInput::make('email')
|
||||||
@@ -58,7 +63,7 @@ class UserResource extends Resource
|
|||||||
// ->ignore($get('id')); // Ignore current record during updates
|
// ->ignore($get('id')); // Ignore current record during updates
|
||||||
// })
|
// })
|
||||||
->reactive()
|
->reactive()
|
||||||
//->prefix(fn ($get) => $get('name') ?? null)
|
// ->prefix(fn ($get) => $get('name') ?? null)
|
||||||
// ->suffix('@cripumps.com')
|
// ->suffix('@cripumps.com')
|
||||||
->maxLength(255),
|
->maxLength(255),
|
||||||
Forms\Components\DateTimePicker::make('email_verified_at'),
|
Forms\Components\DateTimePicker::make('email_verified_at'),
|
||||||
@@ -73,7 +78,7 @@ class UserResource extends Resource
|
|||||||
// ->relationship('roles', 'name'),
|
// ->relationship('roles', 'name'),
|
||||||
Forms\Components\Select::make('roles')
|
Forms\Components\Select::make('roles')
|
||||||
->relationship('roles', 'name')
|
->relationship('roles', 'name')
|
||||||
//->relationship(name: 'roles', titleAttribute: 'name')
|
// ->relationship(name: 'roles', titleAttribute: 'name')
|
||||||
// ->saveRelationshipsUsing(function (Model $record, $state) {
|
// ->saveRelationshipsUsing(function (Model $record, $state) {
|
||||||
// $record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => getPermissionsTeamId()]);
|
// $record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => getPermissionsTeamId()]);
|
||||||
// })
|
// })
|
||||||
@@ -101,6 +106,7 @@ class UserResource extends Resource
|
|||||||
$paginator = $livewire->getTableRecords();
|
$paginator = $livewire->getTableRecords();
|
||||||
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
|
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
|
||||||
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
|
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
|
||||||
|
|
||||||
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
|
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
|
||||||
}),
|
}),
|
||||||
Tables\Columns\TextColumn::make('plant.name')
|
Tables\Columns\TextColumn::make('plant.name')
|
||||||
@@ -165,14 +171,14 @@ class UserResource extends Resource
|
|||||||
->label('Import Users')
|
->label('Import Users')
|
||||||
->color('warning')
|
->color('warning')
|
||||||
->importer(UserImporter::class)
|
->importer(UserImporter::class)
|
||||||
->visible(function() {
|
->visible(function () {
|
||||||
return Filament::auth()->user()->can('view import user');
|
return Filament::auth()->user()->can('view import user');
|
||||||
}),
|
}),
|
||||||
ExportAction::make()
|
ExportAction::make()
|
||||||
->label('Export Users')
|
->label('Export Users')
|
||||||
->color('warning')
|
->color('warning')
|
||||||
->exporter(UserExporter::class)
|
->exporter(UserExporter::class)
|
||||||
->visible(function() {
|
->visible(function () {
|
||||||
return Filament::auth()->user()->can('view export user');
|
return Filament::auth()->user()->can('view export user');
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user