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

This commit is contained in:
dhanabalan
2026-02-03 17:01:56 +05:30
parent 1931412511
commit 8e7030b80b

View File

@@ -5,7 +5,7 @@ namespace App\Filament\Resources;
use App\Filament\Exports\UserExporter;
use App\Filament\Imports\UserImporter;
use App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource\RelationManagers;
use App\Models\Plant;
use App\Models\User;
use Filament\Facades\Filament;
use Filament\Forms;
@@ -36,16 +36,21 @@ class UserResource extends Resource
->relationship('plant', 'name')
->nullable()
->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 () {
return optional(User::latest()->first())->plant_id;
}),
Forms\Components\TextInput::make('name')
->required()
->autofocus()
//->reactive()
// ->reactive()
->live(debounce: 600)
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('email', $state . '@cripumps.com');
$set('email', $state.'@cripumps.com');
})
->maxLength(255),
Forms\Components\TextInput::make('email')
@@ -58,7 +63,7 @@ class UserResource extends Resource
// ->ignore($get('id')); // Ignore current record during updates
// })
->reactive()
//->prefix(fn ($get) => $get('name') ?? null)
// ->prefix(fn ($get) => $get('name') ?? null)
// ->suffix('@cripumps.com')
->maxLength(255),
Forms\Components\DateTimePicker::make('email_verified_at'),
@@ -73,7 +78,7 @@ class UserResource extends Resource
// ->relationship('roles', 'name'),
Forms\Components\Select::make('roles')
->relationship('roles', 'name')
//->relationship(name: 'roles', titleAttribute: 'name')
// ->relationship(name: 'roles', titleAttribute: 'name')
// ->saveRelationshipsUsing(function (Model $record, $state) {
// $record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => getPermissionsTeamId()]);
// })
@@ -101,6 +106,7 @@ class UserResource extends Resource
$paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}),
Tables\Columns\TextColumn::make('plant.name')
@@ -165,14 +171,14 @@ class UserResource extends Resource
->label('Import Users')
->color('warning')
->importer(UserImporter::class)
->visible(function() {
->visible(function () {
return Filament::auth()->user()->can('view import user');
}),
ExportAction::make()
->label('Export Users')
->color('warning')
->exporter(UserExporter::class)
->visible(function() {
->visible(function () {
return Filament::auth()->user()->can('view export user');
}),
]);