如何在laravel 5中插入多行?

时间:2016-10-18 11:20:41

标签: laravel laravel-5

我想插入一个带有id的数组: create.blade:

{{ Form::open(array('route' => 'Charge.store','method'=>'POST')) }}
        <select id="disabledSelect" class="form-control" name="Facture_id">
            <option value="{{ $Facture->id }}" >{{ $Facture->Num }}</option>
        </select>
        <br/>
        <div class="form-inline">
            <div class="form-group">
                <input type="text" class="form-control" name="rows[0][Title]" placeholder="libelé"/>
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="rows[0][Quantity]" placeholder="Quantité"/>
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="rows[0][Price]" placeholder="Prix unitaire "/>
            </div>
            <div class="form-group">
                <input type="button" class="btn btn-default" value="Ajouter" onclick="createNew()" />
            </div>
            <div id="mydiv"></div>
        </div>
        <br/>

        <div class="form-group">
            <input type="submit" value="Ajouter" class="btn btn-info">
            <a href="{{ route('Facture.index') }}" class="btn btn-default">Cancel</a>
        </div>
        {{ Form::close() }}

<script>
    var i = 2;

    function createNew() {
        $("#mydiv").append('<div class="form-group">'+'<input type="text" name="rows[' + i +'][Title]" class="form-control" placeholder="libelé"/>'+
                '</div>'+'<div class="form-group">'+'<input type="text" name="rows[' + i +'][Quantity]" class="form-control" placeholder="Quantité"/>'+'</div>'+'<div class="form-group">'+'<input type="text" name="rows[' + i +'][Price]" class="form-control" placeholder="Prix unitaire "/>'+'</div>'+'<div class="form-group">'+
                '<input type="button" name="" class="btn btn-default" value="Ajouter" onclick="createNew()" />'+
                '</div><br/>');
        i++;
    }
</script>

这是我的控制器,当我尝试提交表单时,它会注入值为0的行。 我该怎么办 ?我尝试使用elequent bolk数据,但问题仍然存在:

enter image description here

public function store(Request $request)
{
    // validated input request
    $this->validate($request, [
        'Facture_id' => 'required',


    ]);

    // create new task
    $rows = $request->input('rows');
    foreach ($rows as $row)
    {
        $Charges[] = new Charge(array(
            'course_id'=>$request->input('Facture_id'),
            'Title'=>$row['Title'],
            'Quantity'=>$row['Quantity'],
            'Price'=>$row['Price'],

        ));
    }
    Charge::create($Charges);
    return redirect()->route('Charge.index')->with('success', 'Your task added successfully!');
}

1 个答案:

答案 0 :(得分:4)

您可以使用insert()方法:

    foreach ($rows as $row)
{
    $charges[] = [
        'course_id' => $request->input('Facture_id'),
        'Title' => $row['Title'],
        'Quantity' => $row['Quantity'],
        'Price' => $row['Price'],
    ];
}

Charge::insert($charges);

不要忘记将您使用的所有列名添加到$fillable数组中:

$fillable = ['course_id', 'Title', 'Quantity', 'Price'];