Hard to help when you did not say what is the error.
Saving Many to Many relationship
Saving Many to Many relationship
Please assist I am new to Laravel and have been stuck with this problem for a week.
I have three tables deliveries, suppliers and payments
A supplier hasMany deliveries A delivery BelongsTo a supplier
Deliveries happen daily and Suppliers are paid for the deliveries periodically.
I would like to sum all delivery.value for suppliers and make one payment. This payment is stored in the payment table. I would like to be able to go back and identify which deliveries were paid for by a specific payment.
My Supplier Model
id name phone 1 John 777777 2 Mary 99999
Delivery model
id supplier_id value 1 1 10 2 2 20 3 1 10 4 2 20
Disbursment
id due_amount delivery_id payment_status payment_reference 1 20 1,3 0 payment1 2 40 2,4 0 Payment2
I have managed to get this far with the controller
public function payment ()
{
$request->validate([
'due_amount'=>'required',
'delivery_id'=>'required',
// 'payment_status'=> 'required',
]);
\DB::transaction(function() {
$suppliers = Supplier::get();
foreach ($suppliers as $key => $supplier) {
$due_amount = Delivery::where('supplier_id',$supplier->id)
->where('due_amount', '>', 0)
->sum('due_amount');
$deliveries_id = Delivery::where('supplier_id',$supplier->id)
->where('due_amount', '>', 0)
->pluck('id');
$disbursment = new Disbursment([
'deliveries_id' =>$deliveries_id,
'due_amount' =>$due_amount,
'payment_status' => 0,
]);
$disbursment->save();
$disbursment->deliveries()->syncWithoutDetaching($deliveries_id);
}
});
}
That doesnt seem to be working. Any guidance will be highly appreciated. Thank you guys
Please or to participate in this conversation.