Поле "" не имеет значения по умолчанию (laravel 5.6.33) (firstorcreate)

SQLSTATE[HY000]: General error: 1364 Field 'almt_kec' doesn't have a default value (SQL: insert into kecamatans (nm_kec, nm_camat, updated_at, created_at) values (Bangkinang, Pak Camat, 2018-10-26 04:08:32, 2018-10-26 04:08:32))

это мой код

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Kecamatan;

class KecamatanController extends Controller
{
  public function index(){
    $kecamatans = Kecamatan::orderBy('created_at', 'DESC')->paginate(10);
    return view('kecamatan.index', compact('kecamatans'));
  }

  public function store(Request $request){
      //validasi
      $this->validate($request, [
        'namaKecamatan'=>'required|string|max:50',
        'namaCamat' => 'required|string|max:50',
        'alamatKantor' => 'required|string|max:50',
        'teleponKantor' => 'required|string|max:50',
        'keteranganKecamatan' => 'nullable|string'
      ]);

    try{
      $kecamatans = Kecamatan::firstOrCreate([
        'nm_kec' => $request->namaKecamatan
      ],[
        'nm_camat' => $request->namaCamat
      ],[
        'almt_kec' => $request->alamatKantor
      ],[
        'tlp_kec' => $request->teleponKantor
      ],[
        'ket_kec' =>$request->keteranganKecamatan
      ]);
      return redirect()->back()->with(['success' => 'Kecamatan : ' .$kecamatans->nm_kec .
      ' Ditambahkan']);
    } catch (\Exception $e) {
      return redirect()->back()->with(['error' => $e->getMessage()]);
    }
  }
    //
}

это моя модель

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Kecamatan extends Model
{
  protected $fillable = ['nm_kec','nm_camat','almt_kec','tlp_kec','ket_kec'];
    //
}

это мой взгляд

<form role="form" action="{{ route('kecamatan.store')}}" method="POST">
    @csrf
    <div class="box-body">
      <div class="form-group">
        <label for="input">Nama Kecamatan</label>
        <input type="text" class="form-control {{ $errors->has('namaKecamatan') ? 'is-invalid':''}}"
         name="namaKecamatan" id="namaKecamatan" required>
      </div>
      <div class="form-group">
        <label for="input">Nama Camat</label>
        <input id="namaCamat" type="text" class="form-control {{ $errors->has('namaCamat') ? 'is-invalid':''}}"
        name="namaCamat">
      </div>
      <div class="form-group">
        <label for="input">Alamat Kantor</label>
        <textarea id="alamatKantor" class="form-control {{ $errors->has('alamatKantor') ? 'is-invalid':''}}"
        rows="3"
        placeholder="Alamat Kantor Camat" name="alamatKantor"></textarea>
      </div>
      <div class="form-group">
        <label for="input">No.Tlp/Hp</label>
        <input id="teleponKantor" type="text" class="form-control {{ $errors->has('teleponKantor') ? 'is-invalid':''}}"
        name="teleponKantor">
      </div>
      <div class="form-group">
        <label for="input">Keterangan</label>
        <textarea id="keteranganKecamatan" class="form-control {{ $errors->has('keteranganKecamatan') ? 'is-invalid':''}}"
        rows="3" placeholder="Keterangan tambahan yang dibutuhkan" name="keteranganKecamatan"></textarea>
      </div>
    </div>
    <div class="box-footer">
      <button class="btn btn-primary">Tambah</button>
    </div>
    @slot('footer')
  </form>

можешь помочь мне ?

также поделитесь конструктором схем, который у вас есть?

Niklesh Raut 26.10.2018 06:51

ваш validator работает нормально? Я думаю, вы передаете столбец almt_kec с нулевым значением.

Kapitan Teemo 26.10.2018 06:53
0
2
616
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

The firstOrCreate method will attempt to locate a database record using the given column / value pairs. If the model can not be found in the database, a record will be inserted with the attributes from the first parameter, along with those in the optional second parameter.

$kecamatans = Kecamatan::firstOrCreate([
    'nm_kec' => $request->namaKecamatan, // A
], [
    'nm_camat' => $request->namaCamat,   // B
], [
    'almt_kec' => $request->alamatKantor,
], [
    'tlp_kec' => $request->teleponKantor,
], [
    'ket_kec' => $request->keteranganKecamatan,
]);

Итак, вы пытаетесь найти nm_kec (А), затем вставляете только nm_camat (B).

Попробуйте сменить свой $kecatamans:

$kecamatans = Kecamatan::firstOrCreate([
    'nm_kec'   => $request->namaKecamatan,
    'nm_camat' => $request->namaCamat,
    'almt_kec' => $request->alamatKantor,
    'tlp_kec'  => $request->teleponKantor,
    'ket_kec'  => $request->keteranganKecamatan,
]);

Mungkin karena Pak Camat nya lagi gak ada di kantor haha ​​..

Другие вопросы по теме