Warm tip: This article is reproduced from stackoverflow.com, please click
laravel-6 php

How to Add Multiple Child Row Data together with Parent Row in Laravel Blade

发布于 2020-04-13 09:53:07

I've been trying to work the adding of multiple data through blade. In my blade form, I'm using the clone, adding rows. For example, one requisition has many items.

My blade view enter image description here

Blade View Code

<form autocomplete="off" method="POST" action="{{ route("requisition.store") }}" enctype="multipart/form-data">
        @csrf
        <div class="form-group">
            <label class="required" for="purpose">Purpose</label>
            <input class="form-control" type="text" name="purpose" id="purpose" required>
            <span class="help-block"></span>
        </div>

        <div class="form-group">
            <label class="required" for="requisition_date">Date</label>
            <input class="form-control" type="date" name="requisition_date" id="requisition_date" required>
            <span class="help-block"></span>
        </div>

        <div class="form-group">
            <label class="required" for="requested_by">Released By</label>
            <input class="form-control" type="text" name="requested_by" id="requested_by" required>
            <span class="help-block"></span>
        </div>

        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Qty</th>    
                    <th>Unit</th>    
                    <th>Description</th>   
                    <th>
                        <a href="#" class="addRow"><i class="fas fa-plus"></i></a>    
                    </th> 
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <input type="number" name="rows[0][qty]" class="form-control quantity" required>  
                    </td>    
                    <td>
                        <input type="text" name="rows[0][unit]" class="form-control" required>  
                    </td>    
                    <td>
                        <input type="text" name="rows[0][description]" class="form-control" required>  
                    </td> 
                    <td>

                    </td>   
                </tr> 
            </tbody>    
        </table>



        <div class="form-group">
            <button class="btn btn-danger" type="submit">
                Save
            </button>
        </div>
    </form>

Script code for cloning

function addRow()
{
    var tr = '<tr>'+
    '<td><input type="number" name="rows[0][qty]" class="form-control quantity"></td>'+
    '<td><input type="text" name="rows[0][unit]" class="form-control quantity"></td>'+
    '<td><input type="text" name="rows[0][description]" class="form-control quantity"></td>'+
    '<td><a class="btn btn-danger remove"><i class="fas fa-times"></i></a></td>'+
    '</tr>';
    $('tbody').append(tr);
  }

And my store controller code

public function store(Request $request)
{
   $requisition = Requisition::create([
            'purpose' => $request->input('purpose'),
            'requisition_date' => $request->input('requisition_date'),
            'requested_by' => $request->input('requested_by')
        ]);  

        $rows = $request->input('rows');

        foreach ($rows as $row)
        {
            $items[] = [
                'requisition_id' => $requisition->id,
                'qty' => $row['qty'],
                'unit' => $row['unit'],
                'description' => $row['description']
            ];
        }

        RequisitionItem::insert($items);

}

My problem in my store controller code is that it is only add one item instead of more than one. My models: Requisition table = 'id', 'purpose', 'requisition_date', 'requested_by', 'requisition_date' RequisitionItem table = 'id', 'requisition_id', 'qty', 'unit', 'description'

dd($rows) results

enter image description here

Questioner
Boss Pogs
Viewed
14
Akhtar Munir 2020-02-03 15:48

I think the simplest way is to use your append inputs and blade inputs both like this...

name="qty[]"

Instead of

name="rows[0][qty]"

And in controller loop through one field that will always be added. For example;

for($i = 0; $i < sizeof($request->qty); $i++)
    {
        RequisitionItem::insert([
            'requisition_id' => $requisition->id,
            'qty' => $request->qty[$i],
            'unit' => $request->unit[$i],
            'description' => $request->description[$i]
        ]);
    }

You can also follow this procedure as well,

  for($i = 0; $i < sizeof($request->qty); $i++)
    {
        $items[] = [
            'requisition_id' => $requisition->id,
            'qty' => $request->qty[$i],
            'unit' => $request->unit[$i],
            'description' => $request->description[$i]
        ];
    }

  RequisitionItem::insert($items);