首页 文章

字段“”没有默认值(laravel 5.6.33)(firstOrCreate)

提问于
浏览
0

SQLSTATE [HY000]:常规错误:1364字段'almt_kec'没有默认值(SQL:插入kecamatans(nm_kec,nm_camat,updated_at,created_at)值(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>

你能帮助我吗 ?

1 回答

  • 0

    firstOrCreate方法将尝试使用给定的列/值对定位数据库记录 . 如果在数据库中找不到该模型,则将插入一条记录,其中包含第一个参数的属性以及可选的第二个参数中的属性 .

    $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_kecA ),然后只插入 nm_camatB ) .

    尝试更改 $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 ..

相关问题