Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert multiple rows in laravel 5?

I want to insert an array with an 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 bolk data , but the problem remain the same:

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!');
}
like image 540
Ahmed Imad Touil Avatar asked Oct 18 '16 11:10

Ahmed Imad Touil


People also ask

How can I insert multiple rows in a single query in database?

INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

How to insert data in Multiple table in Laravel?

First, create a proper relationship with your models ( one to many, etc. ) Second, you can use the DB::transact to insert data into multiple tables in DB. It has it's own advantages rather than just inserting Fk in tables to fill in the data. For further info you can search in google.


1 Answers

You can use insert() method:

    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 $fillable array:

$fillable = ['course_id', 'Title', 'Quantity', 'Price'];
like image 52
Alexey Mezenin Avatar answered Oct 08 '22 21:10

Alexey Mezenin