7

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; } } } 

1 Answer 1

25

Just add this:

public $timestamps = false; 

In your model and you are good to go.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for good support. Hope to more cooperation with you.
With this can i go ahead and delete the $table->timestamps() in the migrations? Or do i leave them even after applying your answer
The reason why to add this to the model is in case you don't have timestamps in your table. So yeah, you can delete them since your model won't use them. But if you need them, just leave them as they are, but having timestamps = false will avoid updating them.
So, important, yet absent from beginners' laravel documentation. hmmm...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.