Aya Alghadban
Many to Many Relation
how can i custom many to many relationship in filament
i have this code
class CreateProduct extends CreateRecord
{
protected static string $resource = ProductResource::class;
protected function handleRecordCreation(array $data): Product
{
return DB::transaction(function () use ($data) {
$product = Product::create($data);
if (isset($data['units'])) {
foreach ($data['units'] as $unitData)
{
ProductUnit::create([
'product_id' => $product->id,
'unit_id' => $unitData['unit_id'],
'price' => $product['price'],
]);
}
}
return $product;
});
}
}
class EditProduct extends EditRecord
{
protected static string $resource = ProductResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
protected function handleRecordUpdate(Model $record, array $data): Product
{
Log::info("message");
return DB::transaction(function () use ($record, $data) {
$record->update($data);
foreach ($data['units'] as $unitData) {
ProductUnit::updateOrCreate(
[
'product_id' => $record->id,
'unit_id' => $unitData['unit_id'],
'price' => $unitData['price']
]
);
}
$unitIds = array_column($data['units'], 'unit_id');
$record->units()->whereNotIn('unit_id', $unitIds)->detach();
return $record;
});
}
}
when i create and edit product with units i saw this error
SQLSTATE[HY000]: General error: 1364 Field 'price' doesn't have a default value
how can is solve that
9 replies
help
I have three tablets in the data base
User, admin, and company employee
I want to build a filament exclusively for the company's employees
The log account means that the only person entering the dashboard is a company employee
I tried to use Filament, but the account was registered in the user table
22 replies