php – laravel orm update document-ThrowExceptions

Exception or error:

I have been trying to update a document using laravel ORM but getting exception stating a unknown column updated_at.

This column is not in database table, and neither have I mentioned in the query.

$foo = ProductMatchUnmatchHistory::where('product_match_unmatches_id', $pmu->id)
                    ->update(['is_latest'=>0]);

ProductMatchUnmatchHistory model:

protected $fillable = [
    'product_match_unmatches_id',
    'human_verdict',
    'ai_result',
    'updated_on',
    'updated_by',
    'is_latest'
];

The table has the same columns as in $fillable array, with additional id primary key.

Here’s the exception message:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: update `product_match_unmatch_histories` set `is_latest` = 0, `updated_at` = 2019-11-20 12:20:02 where `product_match_unmatches_id` = 1759)

I have no clue from where the updated_at column is added to the query.

How to solve:

If you don’t want to use the updated_at and created_at fields from Laravel’s Eloquent models, edit your ProductMatchUnmatchHistory.php model and set $timestamps = false.

class ProductMatchUnmatchHistory extends Model
{
    //....
    $timestamps = false;
    //....
}

Additional Info

Laravel Models automatically set a created_at field when an entry is created and update the updated_at column when you do any changes to a row. This is handled through the $timestamps field on the model. If set to true, which is default, laravel will assume that your models have created_at and updated_at fields.

In most cases this is pretty helpful. You can easily add these fields in your migration by just adding $table->timestamps();.

See https://laravel.com/docs/5.8/eloquent#eloquent-model-conventions
(under Timestamps)

Answer:

please change

you must define table name

protected $table = 'your table name';

protected $fillable = [
    'product_match_unmatches_id',
    'human_verdict',
    'ai_result',
    'updated_on',
    'updated_by',
    'is_latest'
];
$timestamps = false;

Answer:

First please check in your table updated_at exist? because sql error say column not found

Answer:

@Azima you should added the column in your table otherwise you will get this error, because eloquent updating that column and its not present in table or you can $timestamps=false in your Model

Leave a Reply

Your email address will not be published. Required fields are marked *