Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

G-254's avatar
Level 1

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

0 likes
4 replies
bugsysha's avatar

Hard to help when you did not say what is the error.

G-254's avatar
Level 1

Hi, Thanks for taking your time to read. Basically the code I have written seems to do nothing. But on tinker I can get results from the following ;-

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

What I am trying to do is save the results in a payments table. So my question is how do I save these data to the payments table and be able to identify say the 2 deliveries that made us the 1 payment.

My route:

Route::get('tests','DeliveryDisbursmentController@payment');

My Controller

use App\Supplier; use App\Delivery; use App\Delivery_Disbursment;

use DB;

class DeliveryDisbursmentController extends Controller {

public function payment(Request $request) {

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


            $delivery_disbursment = new delivery_disbursment([
                        'deliveries_id'   =>$deliveries_id,
                        'due_amount'      =>$due_amount,
                        'payment_status' => 0,
                     ]);

            $delivery_disbursment->save();

                  $delivery_disbursment->deliveries()->syncWithoutDetaching($deliveries_id);
          }

          return redirect('/tests')->with('success', ' Saved!');


   }

}

When i call

http://project7.test/tests

I get the following message;-

This page isn’t workingproject7.test redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

I hope thats clearer.

Wakanda's avatar

try

return redirect(route('tests.index')->with('success', ' Saved!');

1 like
bugsysha's avatar

The part where you are saving looks OK. But the error is clear. You have a redirect loop where you constantly redirect and the process is being killed cause there is no way out of it.

Route::get('tests','DeliveryDisbursmentController@payment');
return redirect('/tests')->with('success', ' Saved!');

These two lines from your code can not coexist. You need to redirect to some other page.

Please or to participate in this conversation.