gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:40 25 4
gpt4 key购买 nike

当我想更新数据时,laravel 仍然显示列 id 但我没有在我的数据库上使用列 id,并且我已经更改了 Controller

这是我的表迁移

  $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';

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

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

我得到这样的错误

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 默认使用 id 作为主键。您可以在您的模型上覆盖它:

Class YourClass extends Model {

protected $primaryKey = "id_barang";

// rest of your class
}

关于php - SQLSTATE[42S22] : Column not found: 1054 Unknown column 'id' in 'where clause' Id is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56719035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com