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');

View File

@@ -18,6 +18,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 ItemResource extends Resource
{
@@ -41,6 +42,9 @@ class ItemResource extends Resource
// ->preload()
// ->nullable(),
->reactive()
->default(function () {
return optional(Item::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) {
@@ -64,7 +68,7 @@ class ItemResource extends Resource
->required()
->placeholder('Scan the valid code')
->autofocus(true)
->unique(ignoreRecord: true)
// ->unique(ignoreRecord: true)
->alphaNum()
->minLength(6)
// ->autocapitalize('characters')
@@ -95,7 +99,12 @@ class ItemResource extends Resource
'class' => $get('iCodeError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('iCodeError') ? $get('iCodeError') : null)
->hintColor('danger'),
->hintColor('danger')
->rule(function (callable $get) {
return Rule::unique('items', 'code')
->where('plant_id', $get('plant_id'))
->ignore($get('id')); // Ignore current record during updates
}),
Forms\Components\TextInput::make('hourly_quantity')
->required()
->label('Hourly Quantity')
@@ -183,7 +192,8 @@ class ItemResource extends Resource
])
->headerActions([
ImportAction::make()
->importer(ItemImporter::class),
->importer(ItemImporter::class)
->maxRows(100000),
ExportAction::make()
->exporter(ItemExporter::class),
]);

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')

View File

@@ -71,6 +71,9 @@ class PlantResource extends Resource
->relationship('company', 'name')
->required()
->reactive()
->default(function () {
return optional(Plant::latest()->first())->company_id;
})
->disabled(fn (Get $get) => !empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$companyId = $get('company_id');

View File

@@ -4,6 +4,7 @@ namespace App\Filament\Resources;
use App\Filament\Imports\ShiftImporter;
use App\Filament\Resources\ShiftResource\Pages;
use App\Models\Plant;
use App\Models\Shift;
use Carbon\Carbon;
use Filament\Forms;
@@ -16,6 +17,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 ShiftResource extends Resource
{
@@ -38,6 +40,9 @@ class ShiftResource extends Resource
->required()
// ->nullable()
->reactive()
->default(function () {
return optional(Shift::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) {
@@ -62,6 +67,9 @@ class ShiftResource extends Resource
->required()
// ->nullable()
->reactive()
->default(function () {
return optional(Shift::latest()->first())->block_id;
})
->disabled(fn (Get $get) => !empty($get('id')))
// ->options(fn (callable $get) =>
// \App\Models\Block::where('plant_id', $get('plant_id'))
@@ -109,17 +117,17 @@ class ShiftResource extends Resource
}
else
{
$exists = Shift::where('plant_id', $get('plant_id'))
->where('block_id', $get('block_id'))
->where('name', $nameId)
->latest()
->exists();
if ($exists)
{
$set('start_time', null);
$set('sNameError', 'Scanned name already exist.');
return;
}
// $exists = Shift::where('plant_id', $get('plant_id'))
// ->where('block_id', $get('block_id'))
// ->where('name', $nameId)
// ->latest()
// ->exists();
// if ($exists)
// {
// $set('start_time', null);
// $set('sNameError', 'Scanned name already exist.');
// return;
// }
$set('sNameError', null);
}
})
@@ -127,7 +135,13 @@ class ShiftResource extends Resource
'class' => $get('sNameError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('sNameError') ? $get('sNameError') : null)
->hintColor('danger'),
->hintColor('danger')
->rule(function (callable $get) {
return Rule::unique('shifts', 'name')
->where('plant_id', $get('plant_id'))
->where('block_id', $get('block_id'))
->ignore($get('id')); // Ignore current record during updates
}),
Forms\Components\TimePicker::make('start_time')
->required()
->label('Start Time')
@@ -146,18 +160,18 @@ class ShiftResource extends Resource
}
else
{
$exists = Shift::where('plant_id', $get('plant_id'))
->where('block_id', $get('block_id'))
->where('name', $get('name'))
->latest()
->exists();
if ($exists)
{
$set('start_time', null);
$set('sStartTimeError', null);
$set('sNameError', 'Scanned name already exist.');
return;
}
// $exists = Shift::where('plant_id', $get('plant_id'))
// ->where('block_id', $get('block_id'))
// ->where('name', $get('name'))
// ->latest()
// ->exists();
// if ($exists)
// {
// $set('start_time', null);
// $set('sStartTimeError', null);
// $set('sNameError', 'Scanned name already exist.');
// return;
// }
$set('sStartTimeError', null);
}
})