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.
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);
}
@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?
@ChiefJS Please provide a use case to be able understand your requirement.
@ChiefJS Please provide input form snippet from which you want to insert multiple model.
@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>
@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.
@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();
}
@premsaurav Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
@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
If you are talking about foreign keys in general, yes integrity check will be performed for all the operations associated with the model.
Please sign in or create an account to participate in this conversation.