laravel 如何在laravel 5中插入多行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40106855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:37:56  来源:igfitidea点击:

How to insert multiple rows in laravel 5?

laravellaravel-5

提问by Ahmed Imad Touil

I want to insert an array with an id : create.blade :

我想插入一个带有 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>

here is my controller , when I tried to submit the form , it inject rows with value of 0. What should I do ? I tried to use elequent bolkdata , but the problem remain the same:

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

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!');
}

采纳答案by Alexey Mezenin

You can use insert()method:

您可以使用insert()方法:

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

Charge::insert($charges);

Don't forget to add all column names you use to a $fillablearray:

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

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