I'm trying to insert multiple rows to database, but I got error SQLSTATE[HY000]: General error: 1364 Field 'nilai' doesn't have a default value (SQL: insert into nilai_mapel
(mapel_id
, updated_at
, created_at
) values (4, 2021-03-11 15:56:44, 2021-03-11 15:56:44))`
Here my code:
Controller
public function store(Request $request)
{
$mapel_id = $request->mapel_id;
$nilai = $request->nilai;
foreach($mapel_id as $key => $mapel)
{
$input['mapel_id'] = $mapel;
$input['nilai '] = $nilai[$key];
Nilai::create($input);
}
return redirect()->route('nilai');
}
Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Nilai extends Model
{
protected $table = "nilai_mapel";
protected $fillable = ['mapel_id', 'nilai'];
}
Blade
<form class="form-horizontal" action="{{ route('nilai.store') }}" method="post" enctype="multipart/form-data">
@csrf
<table class="table">
<thead>
<tr>
<th>Mapel</th>
<th>Nilai</th>
<th><a href="javascript:;'" class="btn btn-info addRow">+</a></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select class="form-control" id="mapel_id" name="mapel_id[]">
<option value="0">--Pilih Mapel--</option>
@foreach (App\Mapel::all() as $mapel)
<option value="{{ $mapel->id }}">{{ $mapel->nama_mapel }}</option>
@endforeach
</select>
</td>
<td><input type="number" name="nilai[]" id="nilai" class="form-control" placeholder="Masukkan nilai..."></td>
<td><a href="javascript:;'" class="btn btn-danger deleteRow">-</a></td>
</tr>
</tbody>
</table>
</div>
<div class="card-footer">
<button class="btn btn-sm btn-primary" type="submit"> Simpan Data</button>
</div>
</form>