Tag Archives: Laravel tutorial summary

Laravel model save if the table has no ID field Error [How to Solve]

$model = Test::first();
$model->status = 2;
$model->save();

If the Test model does not have an ID field in the corresponding table, an error will be reported

Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘id’ in ‘where clause’ (SQL: update test set status = 2, test.updated_at = 2020-09-04 15:03:06 where id is null) in file D:\www\wb-mg\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 671

As you can see above, save() is to match the updated data through the id field, so don’t use save() if the table doesn’t have an ID field.