Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove "updated_at" while keeping the "created_at" in Eloquent ORM Laravel

In Laravel I have a model that is created only but not updated. So, I want to remove only the updated_at field. But the created_at field is required.

So how can I remove only the updated_at field while keeping the created_at field?

like image 210
Blip Avatar asked Dec 04 '19 07:12

Blip


Video Answer


2 Answers

In your model add these two lines:

public $timestamps = ["created_at"]; //only want to used created_at column
const UPDATED_AT = null; //and updated by default null set

second way:

public $timestamps = false; //by default timestamp true

add function like this:

public function setCreatedAtAttribute($value) { 
    $this->attributes['created_at'] = \Carbon\Carbon::now(); 
}

for more info about laravel timestamp see

like image 163
Jignesh Joisar Avatar answered Sep 22 '22 02:09

Jignesh Joisar


The answer you checked as the solution is not what the question asked for.

you can drop the updated_at column just in the table creation migration:

Schema::create('sample', function (Blueprint $table)    
    $table->id();
    $table->timestamps();
    $table->dropColumn('updated_at');
});
like image 35
Faramarz Avatar answered Sep 24 '22 02:09

Faramarz