14

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?

4 Answers 4

31

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

from laravel 10 you can use withoutTimestamps() method

$user = User::find(1); $user->withoutTimestamps(function () use ($user) { $user->update(['name' => 'John Doe']); }); 

for more info about laravel timestamp see

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

3 Comments

I liked your first solution
@Blip i also used first solution
4

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

3 Comments

The answer you posted is not an answer to my question at all. I didn't ask about migration. The question is about eloquent ORM. Now coming to what you have answered, I would like to add here that you have first added the updated_at and then removed the column. Would it not be better if you didn't add it in the first place?
@Blip Yeap, you're right, i tought your question is for migrations. the thing is you can't just use updated_at on migrations, it comes with created_at in the timestamps(). jignesh Joisar's answer is the correct solution for ORMs. my google search for migrations led me to this post, so i let this answer stay here maybe it helps someone.
I agree with you. But my suggestion is that you should replace the lines $table->dropColumn('update_at') and $table->timestamps() with a single line $table->timestamp('created_at')->nullable(); as it would be more appropriate to just add one column instead of adding two columns and removing one column.
3

If you look inside the trait Illuminate\Database\Eloquent\Concerns\HasTimestamps you will find many options.

My favourite is setting a constant on your model like this.

const UPDATED_AT = null; 

But you also can overwrite a function that sets an updated_at field like that

public function setUpdatedAt($value): self { // Do nothing. return $this; } 

or, you can overwrite another function like this

public function getUpdatedAtColumn() { return null; } 

Comments

-3

Add the line below to your model. This applies to insert and select queries.

public $timestamps = false; 

If you don't need to create a column as well, remove the below the line in the database/migration file.

$table->timestamps(); 

1 Comment

I want the created_at but don't require the updated_at, You have not provided the solution to that. Your solution results in removal of both created_at and updated_at.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.