Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ChiefJS's avatar

Multiple Insert with one query

How can I do a multiple insert into a table using data from my view? I have several drop down list each related to an input box, I would like to insert the values from both the drop down list and input box into multiple rows.

0 likes
12 replies
mrabbani's avatar

You may do that by using createMany(2D Array). For example:

Tag Model

class Tag extends  Eloquent 
{
              protected $fillable = ['name'];             
}

Create multiple model in one command:

public function createMultipleTag ()
{
        $tagList = [['name'=>'tag1'], ['name'=>'tag2']];
         
          Tag::createMany($tagList);
}          
ChiefJS's avatar

@mrabbani What is tag1 and tag2 in this case? Lets say I have Orange id and Apples id in and I want to input the prices of each, how would I do that with the code you've just provided?

mrabbani's avatar

@ChiefJS Please provide input form snippet from which you want to insert multiple model.

ChiefJS's avatar

@premsaurav @mrabbani

<div class="form-group">
    <div class="col-md-6">
        <div class="form-material">
            <select class="form-control" id="fruit" name="fruit">
                @foreach($fruits as $fruit)
                    <option value="{{ $fruit->id }}">{{ $fruit->name }}</option>

                @endforeach

            </select>
        </div>
    </div>
</div>
<div class="form-group">
    <div class="col-md-3">
        <input class="form-control" type="text" id="price" name="price">

    </div>

</div>
<div class="form-group">
    <div class="col-md-6">
        <div class="form-material">
            <select class="form-control" id="fruit" name="fruit">
                @foreach($fruits as $fruit)
                    <option value="{{ $fruit->id }}">{{ $fruit->name }}</option>

                @endforeach

            </select>
        </div>
    </div>
</div>
<div class="form-group">
    <div class="col-md-3">
        <input class="form-control" type="text" id="price" name="price">

    </div>

</div>
d3xt3r's avatar
d3xt3r
Best Answer
Level 29

@chiefJS Have a look at this thread https://laracasts.com/discuss/channels/laravel/save-multiple-data-in-database Whenever, multiple records of same name is required from a form, as in your case price and fruit, use array [].

<select class="form-control" id="fruit" name="fruit[]">
                @foreach($fruits as $fruit)
                    <option value="{{ $fruit->id }}">{{ $fruit->name }}</option>

                @endforeach

            </select>
<input class="form-control" type="text" id="price" name="price[]">

NOTE: Please refrain from using same id twice in single documents. That is not a valid html. use id="price-1" etc if you are really using this id somewhere, or better use class.

ChiefJS's avatar

@premsaurav Why is the code below not working?

public function savePrice(Request $request) {
    $fruitArray = $request->input('fruit');
    $priceArray = $request->input('price');

    if(count($fruitArray) > count($priceArray))
        $count = count($priceArray);
    else $count = count($fruitArray);

    $items = array();
    for($i = 0; $i < $count; $i++){
        $item = array(
            'fruit_id' => $fruitArray[$i],
            'price' => $priceArray[$i]
        );

        $items[] = $item;
    }

    Price::insert($items);

    return redirect()->back();
}
ChiefJS's avatar

@premsaurav Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

d3xt3r's avatar

@ChiefJS It is exactly what it says. Read more about foreign keys, when can you add, update or delete records which are associated with foreign keys, what are options on update, and/ or onDelete

d3xt3r's avatar

If you are talking about foreign keys in general, yes integrity check will be performed for all the operations associated with the model.

Please or to participate in this conversation.