F
Filament5mo ago
rang

Repeater data record didnt appear in relationship

I have supplierRestock that hasMany restockItem (relationship is defined in supplierRestock model), so i use repeater and try to edit some data inside but i cant find the data only from repeater. I dump the data in create record with handleRecordCreation but find nothing from tableRepeater. when i try remove the relationship() it appear.
Section::make('')
->schema([
Select::make('supplier_id')
->label('Supplier')
->relationship('supplier', 'name')
->required()
->native(false),
TextInput::make('qty_tons')
->required()
->label('Jumlah (ton)')
->numeric()
->minValue(1),
DatePicker::make('date')
->native(false)
->displayFormat('d/m/Y')
->label('Tanggal')
->required(),
])
->columnSpan(4),
Section::make()->schema([
TableRepeater::make('restockItems')
->relationship()
->headers([
Header::make('item_id')
->width('180px')
->label('Kode Item'),
Header::make('qty')
->width('130px')
->label('Jumlah'),
])
->showLabels()
->schema([
Select::make('item_id')
->label('Item')
->options(Item::all()->sortBy('code')->pluck('code', 'id')->toArray())
->placeholder('Pilih')
->inlineLabel()
->native(false)
->required()
->disabledOn('edit'),
TextInput::make('qty')
->label('Jumlah')
->inlineLabel()
->numeric()
->minValue(1)
->required(),
]),
])->columnSpan(6)
Section::make('')
->schema([
Select::make('supplier_id')
->label('Supplier')
->relationship('supplier', 'name')
->required()
->native(false),
TextInput::make('qty_tons')
->required()
->label('Jumlah (ton)')
->numeric()
->minValue(1),
DatePicker::make('date')
->native(false)
->displayFormat('d/m/Y')
->label('Tanggal')
->required(),
])
->columnSpan(4),
Section::make()->schema([
TableRepeater::make('restockItems')
->relationship()
->headers([
Header::make('item_id')
->width('180px')
->label('Kode Item'),
Header::make('qty')
->width('130px')
->label('Jumlah'),
])
->showLabels()
->schema([
Select::make('item_id')
->label('Item')
->options(Item::all()->sortBy('code')->pluck('code', 'id')->toArray())
->placeholder('Pilih')
->inlineLabel()
->native(false)
->required()
->disabledOn('edit'),
TextInput::make('qty')
->label('Jumlah')
->inlineLabel()
->numeric()
->minValue(1)
->required(),
]),
])->columnSpan(6)
with relationship()
without relationship()
3 Replies
Dennis Koch
Dennis Koch5mo ago
All fields with ->relationship() saves directly to the relationship and remove the data afterwards, because it can't be saved to the model. What's the issue you are having? If you want to mutate the data, there are methods like ->mutateRelationshipDataBeforeCreate
rang
rangOP5mo ago
protected function handleRecordCreation(array $data): SupplierRestock
{
dd($data);
// Pick highest number and formating code
$lastNumCode = SupplierRestock::max('num_code');
$num_code = $lastNumCode ? $lastNumCode + 1 : 1;
$newCode = 'SR' . str_pad($num_code, 4, '0', STR_PAD_LEFT);

// New Supplier Restock
$supplierRestock = SupplierRestock::create([
'code' => $newCode,
'num_code' => $num_code,
'supplier_id' => $data['supplier_id'],
'qty_tons' => $data['qty_tons'],
'date' => $data['date'],
]);

// Input each supplier items
foreach ($data['restockItems'] as $item) {
SupplierRestockItem::create([
'supplier_restock_id' => $supplierRestock->id,
'item_id' => $item['item_id'],
'qty' => $item['qty'],
'curr_qty' => $item['qty'],
]);
}

return $supplierRestock;
}
protected function handleRecordCreation(array $data): SupplierRestock
{
dd($data);
// Pick highest number and formating code
$lastNumCode = SupplierRestock::max('num_code');
$num_code = $lastNumCode ? $lastNumCode + 1 : 1;
$newCode = 'SR' . str_pad($num_code, 4, '0', STR_PAD_LEFT);

// New Supplier Restock
$supplierRestock = SupplierRestock::create([
'code' => $newCode,
'num_code' => $num_code,
'supplier_id' => $data['supplier_id'],
'qty_tons' => $data['qty_tons'],
'date' => $data['date'],
]);

// Input each supplier items
foreach ($data['restockItems'] as $item) {
SupplierRestockItem::create([
'supplier_restock_id' => $supplierRestock->id,
'item_id' => $item['item_id'],
'qty' => $item['qty'],
'curr_qty' => $item['qty'],
]);
}

return $supplierRestock;
}
i want to modify the SupplierRestockItem creation manually like this but the relationship() keep giving me undefined array key $data['restockItems']
toeknee
toeknee5mo ago
because restockItems is a relationship I suspect. The relationship should already be created
Want results from more Discord servers?
Add your server