Refactor plant selection options across multiple resources to display plants based on the authenticated user's plant ID, ensuring users only see relevant options.
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Filament\Exports\ItemExporter;
|
||||
use App\Filament\Imports\ItemImporter;
|
||||
use App\Filament\Resources\ItemResource\Pages;
|
||||
use App\Models\Item;
|
||||
use App\Models\Plant;
|
||||
use Filament\Actions\Exports\Enums\ExportFormat;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms;
|
||||
@@ -44,6 +45,10 @@ class ItemResource extends Resource
|
||||
// ->preload()
|
||||
// ->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::pluck('name', 'id')->toArray();
|
||||
})
|
||||
->default(function () {
|
||||
return optional(Item::latest()->first())->plant_id;
|
||||
})
|
||||
@@ -69,7 +74,6 @@ class ItemResource extends Resource
|
||||
Forms\Components\TextInput::make('category')
|
||||
->label('Category')
|
||||
->placeholder('Scan the Category'),
|
||||
|
||||
Forms\Components\TextInput::make('code')
|
||||
->required()
|
||||
->placeholder('Scan the valid code')
|
||||
|
||||
Reference in New Issue
Block a user