SQLSTATE [42S22]:找不到列:1054“ where子句” Id中的未知列“ id”为空

时间:2019-06-22 20:41:45

标签: php mysql laravel laravel-5

当我想更新数据时,laravel仍然显示列ID,但是我没有在数据库中使用列ID,并且我已经更改了控制器

这是我的表迁移

  $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();
        });

这是我的模特

protected $table = 'Tb_Barang';

这是我的控制器,没有任何请求或存储到'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();
}

这是视图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>

我得到这样的错误

  

SQLSTATE [42S22]:找不到列:1054'where中的未知列'id'   子句”(SQL:更新Tb_Barangnama_barang = Barang 02,   Tb_Barangupdated_at = 2019-06-22 13:17:23其中id为空)

2 个答案:

答案 0 :(得分:2)

Laravel的主键默认为id。您可以在模型上覆盖它:

Class YourClass extends Model {

    protected $primaryKey = "id_barang";

    // rest of your class
}

答案 1 :(得分:1)

默认情况下,Laravel使用“ id”作为主键。如果您使用其他名称,则可能需要更改模型中的$ primaryKey:

protected $primaryKey = 'id_barang';

建议您仅将“ id”用作主键,以免给您带来很多麻烦。