Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable updated_at and created_at when saving data in Laravel

I have tried to save data in the model function. When using save() function of the model, I see some error such as below.

Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into account_types (name, updated_at, created_at) values (manufactuer, 2019-08-05 05:33:57, 2019-08-05 05:33:57)) in file F:\HTML&

I have deleted created_at and updated_at columns in the table coz I don't need it. I want to know how I can disable created_at and update_at data saving when using model save() function in the Laravel.

<?php

namespace pshub;

use Illuminate\Database\Eloquent\Model;

class AccountType extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];

    // Create Type Function
    public function createType($type) {
        $existence = $this->where('name', $type)->get();

        if (sizeof($existence) > 0) {
            return $this->where('name', $type);
        } else {
            $this->name = $type;
            $this->save();
            return $this->id;
        }
    }
}
like image 832
Nikolai Wisenov Avatar asked Dec 01 '22 09:12

Nikolai Wisenov


1 Answers

Just add this:

public $timestamps = false;

In your model and you are good to go.

like image 190
nakov Avatar answered Dec 04 '22 00:12

nakov