-1

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>
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Origami
  • 47
  • 2
  • 9
  • $input['nilai '] < notice the space. – flakerimi Mar 11 '21 at 16:20
  • ah... I feel stupid, can't notice a single space lol. anyway, thanks for your help – Origami Mar 11 '21 at 16:22
  • but $request->nilai will always have only one item (index 0), what is your goal? you probably have more issues here.. – Martin Osusky Mar 11 '21 at 16:30
  • `array:3 [▼ "_token" => "oX1bj2ZpwuOpFgKLLhc2um1OAIVh7qFxiMGhiH6c" "mapel_id" => array:2 [▼ 0 => "5" 1 => "4" ] "nilai" => array:2 [▼ 0 => "88" 1 => "44" ] ]` if i dd($request->all()); i got this – Origami Mar 11 '21 at 16:35
  • Its a typo `$input['nilai '] = $nilai[$key];` extra space on `['nilai ']` – STA Mar 11 '21 at 17:03
  • Does this answer your question? [MySql Error: 1364 Field 'display\_name' doesn't have default value](https://stackoverflow.com/questions/18751291/mysql-error-1364-field-display-name-doesnt-have-default-value) – miken32 Mar 11 '21 at 23:06

1 Answers1

1

Field 'nilai' doesn't have a default value

It sounds like your nilai_mapel table has a field named 'nilai' which cannot be null and does not have a default value set. This results in an error when you try to insert a row without addressing this field.

The quickest fix for this is to add this field to your insert query.

The other possibilities are :

You could remove that column if it is no longer needed. You could make it nullable. You could set a default value for it.

James Clark
  • 1,285
  • 2
  • 9
  • 14