vendredi 14 décembre 2018

multidimensional array from Vue to Laravel

I'm having a bit of brain freeze and can't figure out how to get the data from these two forms from VueJS to save with Laravel.

Heres a link to how the form looks - https://imgur.com/732pbyT

Here's the code with VueJS

<label>Reading Type</label>
                <select name="reading_type[]" class="form-control">
                    <option value="">Please Select</option>
                    <option v-for="(type, index) in reading_type" 
                            :value="type.tags"
                            :key="type.index" 
                            :id="type.id"
                    >
                        
                    </option>
                </select>
                <div class="row mt-2">
                    <div class="col">
                    <label>Gas Reading</label>
                    <input type="number" name="reading[]" class="form-control">

                    <label>Water Reading</label>
                    <input type="text" name="reading[]" class="form-control">

                    <label>Electric Reading</label>
                    <input type="number" name="reading[]" class="form-control">
                </div>
                <div class="col">
                    <label>Reading Date</label>
                    <input type="date" class="form-control" name="reading_date[]">

                    <label>Reading Date</label>
                    <input type="date" class="form-control" name="reading_date[]">

                    <label>Reading Date</label>
                    <input type="date" class="form-control" name="reading_date[]">
                </div>

Here's what I have currently with Laravel:

$readings = $request->get('reading');   
$dates = $request->get('reading_date');
$types= $request->get('reading_type');

    for($i = 0; $i < $readings; $i++){
        $reading = new MeterReadings;
        $reading->user_id       = user()->id;
        $reading->entered_by    = user()->name;
        $reading->meter_type    = $types[$i];
        $reading->property_id   = $request->property;
        $reading->reading       = $readings[$i];
        $reading->reading_date  = $dates[$i];
        $reading->save();
    }

But it's not saving or throwing some errors like Undefined offset: 2

Any help greatly appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire