I’m using laravel as my web project, and I have an issue when I update my database.
I have an error message on my postman after posting data. Here is the message.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `data_transaksis` set `jumlah_transaksi` = 10000, `info` = Transaksi 2 Mysql Update, `data_transaksis`.`updated_at` = 2021-05-23 03:59:54 where `id` is null)
This is my function code to update a row on a database.
$dataTransaksi = DataTransaksi::where('transact_id', $request->transact_id)->first(); $dataTransaksi->jumlah_transaksi = $request->jumlah_transaksi; $dataTransaksi->info = $request->info; $dataTransaksi->save(); return response($dataTransaksi, 200)->header('Content-type', 'application/json');
I don’t know what’s going on with my code, i search for ‘transact_id’ on my query builder, but the error message shows that I search for ‘id’. It’s odd. Any solution for my problem?
Edit: This is my table structure. https://imgur.com/gallery/s5Ltmek
Advertisement
Answer
My suggestion is to set your primary key in the model like protected $primaryKey = 'transact_id'
because laravel assumes id
as primary key by default.
Check this reference for more info on laravel and primary keys.