Laravel 多条数据插入数据库
Posted
技术标签:
【中文标题】Laravel 多条数据插入数据库【英文标题】:Laravel multiple data insert into database 【发布时间】:2021-05-11 23:26:41 【问题描述】:我想在 Laravel 中的一个 delivery_notes_id 中插入更多项目。但我收到错误“数组到字符串的转换”。当我用 $lastid 删除代码时,没有错误。 你能帮帮我吗?
$data=$request->all();
$lastid=delivery_note::create($data)->id;
$delivery_note->datum = $data['datum'];
$delivery_note->customer_id = $data['customer'];
if(count($data['produkt']) > 0)
$data2=[];
foreach($data['produkt'] as $item => $value)
array_push($data2, [
'delivery_notes_id'=>$lastid,
'menge'=>$request['menge'][$item],
'einheit'=>$request['einheit'][$item],
'product_id'=>$request['produkt'][$item],
]);
Items::insert($data2);
return $this->index()->with([
'meldung_success' => 'Lieferschein wurde erstellt!'
]);
这是 html 代码。我已经尝试过以下教程https://tsdurjoy.blogspot.com/2019/01/laravel-multiple-data-insert-into_20.html
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="container">
<h4 class="text-center"><a href="/delivery_note"><i class="fas fa-chevron-left"></i></a> neuer LIEFERSCHEIN</h4>
<form class="row g-3" action="/delivery_note" method="post">
@csrf
<table>
<tbody>
<tr>
<label for="datum" class="form-label">Datum</label>
<input type="date" class="form-control" name="datum" id="datum" value=" old('datum') ">
</tr>
<tr>
<label for="customer_id" class="form-label">Kunde</label>
<select id="customer_id" class="form-control" name="customer">
<option selected>Kunde wählen</option>
@foreach (App\Customer::get() as $customers)
<option value="$customers->id ">!! $customers->customer !!</option>
@endforeach
</select>
</tr>
<div id="more">
<tr>
<td>
<label for="menge" class="form-label">Menge</label>
<input type="text" class="form-control" name="menge[]" id="menge" value=" old('menge') ">
</td>
<td>
<label for="einheit" class="form-label">EH</label>
<input type="text" class="form-control" name="einheit[]" id="einheit" value=" old('einheit') ">
</td>
<td>
<label for="produkt" class="form-label">Produkt</label>
<select id="produkt" class="form-control" name="produkt[]">
<option selected>Produkt wählen</option>
@foreach (App\Product::get() as $produkt)
<option value="$produkt->id">!! $produkt->produkt !!</option>
@endforeach
</select>
</td>
</tr>
</div>
</tbody>
</table>
<div class="col-12 mt-2">
<a class="addRow" name="addRow" id="addRow">+ weiteres Produkt</a>
</div>
<div class="col-12 mt-4">
<button type="submit" class="btn btn-primary">SPEICHERN</button>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$('.addRow').on('click', function()
addRow();
);
function addRow()
var tr = '<tr>'+
'<td>'+
'<input type="text" class="form-control" name="menge[]" id="menge" value=" old('menge') ">'+
'</td>'+
'<td>'+
'<input type="text" class="form-control" name="einheit[]" id="einheit" value=" old('einheit') ">'+
'</td>'+
'<td>'+
'<select id="produkt" class="form-control" name="produkt[]">'+
'<option selected>Produkt wählen</option>'+
'@foreach (App\Product::get() as $produkt)'+
'<option value="$produkt->id">!! $produkt->produkt !!</option>'+
'@endforeach'+
'</select>'+
'</td>'+
'</tr>';
$('tbody').append(tr);
;
</script>
【问题讨论】:
试试dd($request->all())
然后告诉我们结果。
也需要你的模型
这是 dd($request->all())array:6 [▼ "_token" => "CEHjanB31WmJ6gxI8Q4BNIraU8wZkxW6QhM095iV" "datum" => "2021-02-11" "customer " => "3" "menge" => array:1 [▼ 0 => "3" ] "einheit" => array:1 [▼ 0 => "kg" ] "produkt" => array:1 [▼ 0 => "2" ] ]
模型项目 belongsTo('App\delivery_note');
请将您的表单添加到代码中
【参考方案1】:
试试这个:
$data=$request->all();
$lastid=delivery_note::create($data)->id;
$delivery_note->datum = $data['datum'];
$delivery_note->customer_id = $data['customer'];
if(count($data['produkt']) > 0)
foreach($requsest->get('produkt') as $item => $value)
$data = new yourModelName();
$data->delivery_notes_id=$lastid;
$data->menge=$request->menge[$item];
$data->einheit=$request->einheit[$item];
$data->product_id=$request->produkt[$item]
$data->save();
return $this->index()->with([
'meldung_success' => 'Lieferschein wurde erstellt!'
]);
【讨论】:
你好,问题加了html代码以上是关于Laravel 多条数据插入数据库的主要内容,如果未能解决你的问题,请参考以下文章
仅当一个字段作为数组提供时,Laravel Eloquent 才插入多条记录