BossPogs
5 months ago
829
4
Laravel

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

Posted 5 months ago by BossPogs

Hello guys. 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

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="qty[]" class="form-control quantity" required>  
                        </td>    
                        <td>
                            <input type="text" name="unit[]" class="form-control" required>  
                        </td>    
                        <td>
                            <input type="text" name="description[]" class="form-control" required>  
                        </td> 
                        <td>
                            <a href="#" class="btn btn-danger remove">
                                <i class="fas fa-times"></i>
                            </a>    
                        </td>   
                    </tr>    
                </tbody>    
            </table>

            <div class="form-group">
                <label for="filename">Upload Image File</label>
                <div class="input-group control-group increment" >
                    <input type="file" name="filename[]" class="form-control">
                    <div class="input-group-btn"> 
                    <button class="btn btn-success" type="button"><i class="glyphicon glyphicon-plus"></i>Add</button>
                    </div>
                </div>
                
                <div class="clone hide">
                    <div class="control-group input-group" style="margin-top:10px">
                    <input type="file" name="filename[]" class="form-control">
                    <div class="input-group-btn"> 
                        <button class="btn btn-danger" type="button"><i class="glyphicon glyphicon-remove"></i> Remove</button>
                    </div>
                    </div>
                </div>
            </div>
           
            <div class="form-group">
                <button class="btn btn-danger" type="submit">
                    Save
                </button>
            </div>
        </form>

And the Store Controller

public function store(Request $request)
{
        $requisition = Requisition::create([
                'purpose' => $request->input('purpose'),
                'requisition_date' => $request->input('requisition_date'),
                'requested_by' => $request->input('requested_by')
            ]);  
            
            
            foreach($requisition as $item)
            {
                $requisition_item = RequisitionItem::create([
                    //still figuring what would be the code
                ]);
            }
}

requistion table = id, purpose, requisition_date, requested_by requisition_item table = id, requisition_id, qty, unit, description

Please sign in or create an account to participate in this conversation.