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?
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
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'); }); 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.$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.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; } 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(); 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.