0

I'm working on a rather complex portion of a little project I'm working on.

I've got an invoice with shipment items (labeled on models as invoice and invoice_item), and often times an shipment will have multiple invoice items. Below is an example of the current shipment item section -

enter image description here

Of course it starts off with only one row (the one with the "Add more" button at the end). And using the shipment controller (right below), I can easily save an shipment_detail to that specific shipment that I am working on.

Shipment Controller

public function store(Shipment $request)
    {

        $this->validate(request(), [

            'pro_number' => 'required',
            'shipment_origin' => 'required'

        ]);

        $user_id = Auth::id();

        $input = $request->all();

        $shipment = new Shipment();
        $shipment->pro_number = request('pro_number');
        $shipment->shipment_origin = request('shipment_origin');
        $shipment->date = request('date');
        $shipment->due_date = request('due_date');
        $shipment->tractor_id = request('tractor_id');
        $shipment->trailer_id = request('trailer_id');
        $shipment->driver_id = request('driver_id');
        $shipment->notes = request('notes');
        $shipment->shipper_no = request('shipper_no');
        $shipment->ship_to = request('ship_to');
        $shipment->ship_from = request('ship_from');
        $shipment->bill_to = request('bill_to');
        $shipment->bill_type = request('bill_type');
        $shipment->load_date = request('load_date');
        $shipment->shipment_status = request('shipment_status');
        $shipment->created_by = $user_id;

        $shipment_detail = new Shipment_Detail();
        $shipment_detail->pieces_number = request('pieces_number');
        $shipment_detail->pieces_type = request('pieces_type');
        $shipment_detail->rate_type = request('rate_type');
        $shipment_detail->weight = request('weight');
        $shipment_detail->hazmat = request('hazmat');
        $shipment_detail->description = request('description');
        $shipment_detail->charge = request('charge');

        $shipment->save();
        $shipment_detail->shipment_id = $shipment->id;
        $shipment->shipment_details()->save($shipment_detail);

        return redirect('/shipments');

    }

However, my problem here is that I cannot save more than one record at a time to the shipment_details. I know this has to have an output as an array but I am unsure how to do that for this particular case. Has anyone run across something like this before?

PS

My form for the shipment_details section looks as such:

<table style="width:100%;" id="freight_bill_items">
                <thead>

                    <td style="width:8%;font-weight:bold;text-align:center;">No. Pieces</td><td style="width:8%; font-weight:bold; text-align:center;">Pieces Type</td><td style="width:16.5%;font-weight:bold;text-align:center;">Rate Type</td><td style="width:16.5%;font-weight:bold;text-align:center;">Weight(lbs)</td><td style="width:16.5%;font-weight:bold;text-align:center;color:red;">HazMat?</td><td style="width:16.5%;font-weight:bold;text-align:center;">Description</td><td style="width:16.5%;font-weight:bold;text-align:center;">Charge</td><td></td>

                </thead>
                <tbody>
                    <tr style="height:40px">
                <td style="width:8%;text-align:center;"><input type="text" name="pieces_number" class="form-control name_list" id="pieces_number" placeholder="No. Pieces"></td><td style="width:16%;text-align:center;"><select style="width:100%; display:inline" class="form-control" name="pieces_type" id="pieces_type">
                    <option selected value="">Please select</option>
     @foreach($piecetypes as $piecetype)
     <option value="{{ $piecetype->id }}">{{ $piecetype->label }}</option>
     @endforeach
                    </select></td><td><select style="width:100%; display:inline" class="form-control" name="rate_type" id="rate_type">
                    <option selected value="">Please select</option>
     @foreach($ratetypes as $ratetype)
     <option value="{{ $ratetype->id }}">{{ $ratetype->label }}</option>
     @endforeach
                    </select></td><td style="width:16.5%;text-align:center;"><input type="text" name="weight" id="weight" class="form-control name_list" placeholder="Weight"></td><td style="width:16.5%;text-align:center;"><select style="width:100%; display:inline" class="form-control" name="hazmat" id="hazmat">
                    <option selected value="0">No</option>
                        <option>Yes</option>
                    </select></td><td style="width:16.5%;text-align:center;"><input type="text" name="description" class="form-control name_list" placeholder="Description" id="description"></td><td style="width:16.5%;text-align:center;"><input type="text" name="charge" class="form-control name_list" placeholder="Charge" id="charge"></td<td><td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
                </tr>
            </tbody>
            </table>
3
  • Use array notation on your input and select elements. stackoverflow.com/a/34753224/5651536 Commented Nov 21, 2017 at 21:04
  • @JohnEllmore - so like this <input type="text" name="shipment_details[][pieces_number]"> in the html form itself, with no changes anywhere else? Commented Nov 21, 2017 at 21:49
  • That’ll allow the data to get sent to the server, yes. You’ll need to modify your controller method in laravel to use the new format, of course. Laravel use for notation for this: laravel.com/docs/5.5/requests#retrieving-input Commented Nov 21, 2017 at 21:58

1 Answer 1

1

use array notation on input types, eg: <input type="text" name="some_name[]">

use a for loop to get all the values and save it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.