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

@@ -15,6 +15,7 @@ use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Components\Section;
use Illuminate\Validation\Rule;
class BlockResource extends Resource
{
@@ -34,7 +35,7 @@ class BlockResource extends Resource
->schema([
Forms\Components\TextInput::make('name')
->required()
->unique(ignoreRecord: true)
// ->unique(ignoreRecord: true)
->placeholder('Scan the valid name')
->autofocus(true)
->reactive()
@@ -54,12 +55,20 @@ class BlockResource extends Resource
'class' => $get('bNameError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('bNameError') ? $get('bNameError') : null)
->hintColor('danger'),
->hintColor('danger')
->rule(function (callable $get) {
return Rule::unique('blocks', 'name')
->where('plant_id', $get('plant_id'))
->ignore($get('id')); // Ignore current record during updates
}),
Forms\Components\Select::make('plant_id')
->relationship('plant', 'name')
// ->unique(ignoreRecord: true)
->required()
->reactive()
->default(function () {
return optional(Block::latest()->first())->plant_id;
})
->disabled(fn (Get $get) => !empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$nameId = $get('plant_id');