简体   繁体   中英

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' Id is null

when i want to update the data, and laravel still showing column id but i'm not using column id on my database, and i has change the Controller

this is my Table mIGRATION

  $table->bigIncrements('id_barang');
            $table->string('nama_barang');
            $table->integer('stok');
            $table->longText('deskripsi_barang');
            $table->dateTime('tgl_kadaluarsa');
            $table->dateTime('tgl_dibuat');
            $table->integer('id_supplier'); 

            $table->timestamps();
        });

this is my model

protected $table = 'Tb_Barang';

this is my controller and nothing request or storing to 'id'

public function update(Request $request, $id_barang)
    {
        $data = ModelBarang::where('id_barang',$id_barang)->first();  //
        $data->nama_barang = $request->nama_barang;
        $data->stok = $request->stok;
        $data->deskripsi_barang = $request->deskripsi_barang;
        $data->tgl_kadaluarsa =$request->tgl_kadaluarsa;
        $data->tgl_dibuat =$request->tgl_dibuat;
        $data->id_supplier = $request->id_supplier;
        $data->save();
}

this is view editbarang.blade.php

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Olah Target</div>
                @foreach($data as $datas)
                <div class="card-body">
                    <form action="{{ route('barang.update', $datas->id_barang) }}"  method="post" >
                        {{ csrf_field() }}
                        {{ method_field('PUT') }}
                        {{-- <div class="form-group row">
                            {{-- <label for="id_barang" class="col-md-4 col-form-label text-md-right">{{ __('ID') }}</label>

                            <div class="col-md-6">
                                <input id="id_barang" type="text" class="form-control @error('name') is-invalid @enderror" name="id_barang" value="{{$datas->id_barang}}" required autocomplete="name" autofocus disabled>

                            </div>
                        </div> --}} 

                        <div class="form-group row">
                            <label for="nama_barang" class="col-md-4 col-form-label text-md-right">{{ __('Nama Barang') }}</label>

                            <div class="col-md-6">
                                <input id="nama_barang" type="text" class="form-control " name="nama_barang" value="{{$datas->nama_barang}}" >

                            </div>
                        </div>



                        <div class="form-group row">
                            <label for="stok" class="col-md-4 col-form-label text-md-right">{{ __('Stok') }}</label>

                            <div class="col-md-6">
                                <input id="stok" type="text" class="form-control" name="stok" value="{{$datas->stok}}" required >

                            </div>
                        </div>


                        <div class="form-group row">
                            <label for="deskripsi_barang" class="col-md-4 col-form-label text-md-right">{{ __('Deskripsi Barang') }}</label>

                            <div class="col-md-6">
                                <input id="deskripsi_barang" type="text" class="form-control @error('name') is-invalid @enderror" name="deskripsi_barang" value="{{$datas->deskripsi_barang}}" required autocomplete="name" autofocus>

                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="tgl_kadaluarsa" class="col-md-4 col-form-label text-md-right">{{ __('Tanggal Kadaluarsa') }}</label>
                            <div class="col-md-6">
                                <input id="tgl_kadaluarsa" type="text" class="form-control " name="tgl_kadaluarsa" value="{{$datas->tgl_kadaluarsa}}">
                            </div>
                        </div>



                        <div class="form-group row">
                            <label for="tgl_dibuat" class="col-md-4 col-form-label text-md-right">{{ __('Tanggal Dibuat') }}</label>

                            <div class="col-md-6">
                                <input id="tgl_dibuat" type="text" class="form-control" name="tgl_dibuat" value="{{$datas->tgl_dibuat}}" required >

                            </div>
                        </div>



                        <div class="form-group row">
                                <label for="id_supplier" class="col-md-4 col-form-label text-md-right">{{ __('ID Supplier') }}</label>

                                <div class="col-md-6">
                                    <input id="id_supplier" type="text" class="form-control" name="id_supplier" value="{{$datas->id_supplier}}" required >

                                </div>
                            </div>

and I get error like this

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update Tb_Barang set nama_barang = Barang 02, Tb_Barang . updated_at = 2019-06-22 13:17:23 where id is null)

Laravel defaults to id for the primary key. You can override this on your Model :

Class YourClass extends Model {

    protected $primaryKey = "id_barang";

    // rest of your class
}

By default Laravel uses "id" for primary key. You might want to change the $primaryKey in the model if you're using different name like so:

protected $primaryKey = 'id_barang';

It is advisable that you just use "id" for primary key to save you a lot of trouble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM