Added combined unique and default value

This commit is contained in:
dhanabalan
2025-04-08 09:13:14 +05:30
parent 4685b7fbb8
commit f066794bca
14 changed files with 84 additions and 39 deletions

View File

@@ -16,6 +16,7 @@ use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Components\Section;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Unique;
class LineResource extends Resource
@@ -38,6 +39,9 @@ class LineResource extends Resource
->required()
// ->nullable(),
->reactive()
->default(function () {
return optional(Line::latest()->first())->plant_id;
})
->disabled(fn (Get $get) => !empty($get('id')))
// ->afterStateUpdated(fn ($set) => $set('block_id', null) & $set('name', null) & $set('start_time', null) & $set('duration', null) & $set('end_time', null))
->afterStateUpdated(function ($state, callable $set, callable $get) {
@@ -88,16 +92,16 @@ class LineResource extends Resource
}
else
{
$exists = Line::where('name', $lineNam)
->where('plant_id', $get('plant_id'))
->exists();
// $exists = Line::where('name', $lineNam)
// ->where('plant_id', $get('plant_id'))
// ->exists();
if ($exists) {
$set('name', null);
$set('lNameError', 'The name has already been taken.');
// $set('lNameError', 'The combination of name and plant ID must be unique.');
return;
}
// if ($exists) {
// $set('name', null);
// $set('lNameError', 'The name has already been taken.');
// // $set('lNameError', 'The combination of name and plant ID must be unique.');
// return;
// }
$set('lNameError', null);
}
})
@@ -105,7 +109,12 @@ class LineResource extends Resource
'class' => $get('lNameError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('lNameError') ? $get('lNameError') : null)
->hintColor('danger'),
->hintColor('danger')
->rule(function (callable $get) {
return Rule::unique('lines', 'name')
->where('plant_id', $get('plant_id'))
->ignore($get('id')); // Ignore current record during updates
}),
Forms\Components\TextInput::make('type')
->required()
->placeholder('Scan the valid type')