issue with repeater and relationship
hey, i have a simple form with simple logic to submit
protected function handleRecordCreation(array $data): Model {
$user = \App\Models\User::firstOrCreate(
['phone' => $data['phone']],
['password' => bcrypt(Str::random(8))]
);
$data['user_id'] = $user->id;
// قیمت نهایی محصولات
$totalProductPrice = 0;
foreach ($data['order_details'] as $item) {
$product = \App\Models\Product::find($item['product_id']);
$price = $product->price_for_quantity($item['quantity']);
$totalProductPrice += ($price * $item['quantity']);
}
// محاسبه هزینه ارسال
if (isset($data['send_id'])) {
$send = \App\Models\Send::find($data['send_id']);
$sendPrice = $send?->price ?? 0;
} else {
$sendPrice = $data['custom_send_price'] ?? 0;
}
// مجموع نهایی قیمت
$data['price'] = $totalProductPrice;
$data['send_price'] = $sendPrice;
$data['final_price'] = $totalProductPrice + $sendPrice;
$record = static::getModel()::create($data);
return $record;
}
but it tries to save related record into order_details. i don't know why is the reason?
13 Replies
because
->relationship('order_details')
?It doesn't matter. Whether it's there or not, it still tries to save a record in order_details, and I get an error. 🫠
Do you have the
order_details
relationship?yes.
public function order_details(): HasMany
{
return $this->hasMany(OrderDetail::class);
}
here it is
remove ->saveRelationshipsUsing(null)
i got same error
Does it show
order_details
?
ahh.. remove ->dehydrated()
no. it looks like this:
array:11 [▼ // app/Filament/Resources/OrderResource/Pages/CreateOrder.php:19
"phone" => "09102174715"
"user_id" => 75
"admin" => 1
"address_id" => "1"
"fname" => "متین"
"lname" => "رجبی"
"send_id" => "1"
"order_type" => "حضوری"
"price" => 150000
"send_price" => "20000"
"final_price" => 170000
]
if you remove dehydrated, it should work
nah.
$data is look like this:
array:11 [▼ // app/Filament/Resources/OrderResource/Pages/CreateOrder.php:19
"phone" => "09102174715"
"user_id" => 75
"admin" => 1
"address_id" => "1"
"fname" => "متین"
"lname" => "رجبی"
"send_id" => "1"
"order_type" => "اینستاگرام"
"price" => 225000
"send_price" => "20000"
"final_price" => 245000
]
yep, order_details relationship should be saved automatically. What is the error when you try to save the record now?
it says:
Field 'product_id' doesn't have a default value (Connection: mysql, SQL: insert into order_details (order_id, updated_at, created_at) values (43, 2025-07-31 11:55:03, 2025-07-31 11:55:03))
check
product_id
in your relationship. I don't think the issue is Filament