Update Record Without Updating Timestamp in Laravel – With Example

December 3, 2022
Laravel
Update Record Without Updating Timestamp in Laravel – With Example

In this article, you will learn how to update record without updating the timestamp in laravel.

How to update Record Without Updating Timestamp in laravel?

Sometimes we need to update the record without updating Timestamp itself. For that reason, laravel included $modal_name->timestamps = false flag.

Example

In the controller file, write the following code below:

<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;

class TestController extends Controller
{

    public function index(Request $request)
    {
        $post = Article::first();

        //don't let the timestamps to get updated
        $post->timestamps = false;

        $post->title = "Article Title";

        $post->save();

    }

You can see that first, we are getting the Article which we want to change, then we are setting laravel eloquent flag timestampsto be false.

So now whatever changes we make to the other columns, created_at and updated_at columns will not get changed.

Conclusion

Hope you have learned today, how to update record without updating timestamps (created_at and updated_at columns) in laravel.

See you in the next one 🙂

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


Icon