Refactor plant selection options in multiple dashboards to filter by authenticated user's plant ID.

This commit is contained in:
dhanabalan
2025-09-30 14:50:16 +05:30
parent 355ce26475
commit fad5bec0de
6 changed files with 136 additions and 112 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
use App\Models\Plant;
use Filament\Facades\Filament;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
@@ -38,8 +39,12 @@ class InvoiceDashboard extends Page
->statePath('filters') // Explicitly set where to store form data
->schema([
Select::make('plant')
->options(Plant::pluck('name', 'id'))
->label('Select Plant')
//->options(Plant::pluck('name', 'id'))
->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();
})
->reactive()
->afterStateUpdated(function ($state) {
session(['selec_plant' => $state]);

View File

@@ -3,6 +3,7 @@
namespace App\Filament\Pages;
use App\Models\Plant;
use Filament\Facades\Filament;
use Filament\Pages\Page;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Filament\Forms\Form;
@@ -35,8 +36,12 @@ class InvoiceQuantityDashboard extends Page
->statePath('filters') // Explicitly set where to store form data
->schema([
Select::make('plant')
->options(Plant::pluck('name', 'id'))
->label('Select Plant')
//->options(Plant::pluck('name', 'id'))
->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();
})
->reactive()
->afterStateUpdated(function ($state) {
session(['selec_plant' => $state]);

View File

@@ -7,6 +7,7 @@ use App\Models\Plant;
use App\Models\ProductionQuantity;
use App\Models\QualityValidation;
use App\Models\StickerMaster;
use Filament\Facades\Filament;
use Filament\Forms\Components\Actions;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Form;
@@ -55,10 +56,14 @@ class ProductionDataSap extends Page implements HasForms
->statePath('data')
->schema([
Select::make('plant_id')
->options(Plant::pluck('name', 'id'))
->label('Plant')
->reactive()
->required()
//->options(Plant::pluck('name', 'id'))
->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();
})
->afterStateUpdated(function ($state, $set, callable $get) {
$plantId = $get('plant_id');
if (!$plantId) {

View File

@@ -3,6 +3,7 @@
namespace App\Filament\Pages;
use App\Models\Plant;
use Filament\Facades\Filament;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
@@ -38,8 +39,12 @@ class ProductionLineStopCount extends Page
->schema([
Select::make('plant')
->options(Plant::pluck('name', 'id'))
->label('Select Plant')
//->options(Plant::pluck('name', 'id'))
->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();
})
->reactive()
->afterStateUpdated(function ($state) {
session(['selected_plant' => $state]);

View File

@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
use App\Filament\Widgets\TrendChartAnalysis;
use App\Models\MfmMeter;
use Filament\Facades\Filament;
use Filament\Pages\Page;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use App\Models\Plant;
@@ -40,7 +41,6 @@ class TrendChartAnalys extends Page
return $form
->statePath('filters')
->schema([
DateTimePicker::make('from_datetime')
->label('From DateTime')
->required()
@@ -60,10 +60,14 @@ class TrendChartAnalys extends Page
session(['to_datetime' => $formatted]);
}),
Select::make('plant')
->options(Plant::pluck('name', 'id'))
->label('Select Plant')
->reactive()
->required()
//->options(Plant::pluck('name', 'id'))
->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();
})
->afterStateUpdated(function ($state, callable $set) {
session(['selected_plant' => $state]);
// When plant changes, also reset meter_name
@@ -71,7 +75,6 @@ class TrendChartAnalys extends Page
session(['selected_meter' => null]);
// dd($state);
}),
Select::make('meter_name')
->options(function ($get) {
$plantId = $get('plant');
@@ -99,7 +102,6 @@ class TrendChartAnalys extends Page
->afterStateUpdated(function ($state) {
session(['parameter' => $state]);
}),
])
->columns(5);
}

View File

@@ -3,6 +3,7 @@
namespace App\Filament\Pages;
use App\Filament\Widgets\TrendLineChart;
use Filament\Facades\Filament;
use Filament\Pages\Page;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use App\Models\Plant;
@@ -39,7 +40,6 @@ class TrendLineAnalysis extends Page
return $form
->statePath('filters')
->schema([
DateTimePicker::make('from_datetime')
->label('From DateTime')
->required()
@@ -59,10 +59,14 @@ class TrendLineAnalysis extends Page
session(['to_datetime' => $formatted]);
}),
Select::make('plant')
->options(Plant::pluck('name', 'id'))
->label('Select Plant')
->reactive()
->required()
//->options(Plant::pluck('name', 'id'))
->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();
})
->afterStateUpdated(function ($state, callable $set) {
session(['selected_plant' => $state]);
// When plant changes, also reset meter_name
@@ -70,7 +74,6 @@ class TrendLineAnalysis extends Page
session(['selected_meter' => null]);
// dd($state);
}),
Select::make('meter_name')
->options(function ($get) {
$plantId = $get('plant');
@@ -98,7 +101,6 @@ class TrendLineAnalysis extends Page
->afterStateUpdated(function ($state) {
session(['parameter' => $state]);
}),
])
->columns(5);
}