How to automatically update the timestamp of parent model in Laravel

Created at 24-Nov-2022, By samar

How to automatically update the timestamp of parent model

In this session, we are going to try to solve the "How to automatically update the timestamp of parent model" puzzle by using the Laravel.

You can auto-update the created_at and update_at columns of model when the child model is created or updated in Laravel. Here we have inbuilt method in Laravel which can be used to update the updated_at and created_at column of parent model, whenever we create or update the child model.

For example: We can use this method in the comment model which contains the primary key of the post table as foreign key (post_id). Whenever we will make any comment on a particular post, it will update the created_at and updated_at column of that post.


class Comment extends Model
    use SoftDeletes;
    protected $touches = ['post'];
    public function post()
        return $this->belongsTo('App\Models\Post');

  • How to define relationship in Laravel?

    To define a relationship, we need first to define the post() method in Comment model. In the post() method, we need to implement the belongsTo() method that returns the result.

  • How many relationships are there in Laravel?

    As you know, in relationships between tables in the database, we usually have 3 types of relationships. These relationships are called one-to-one, one-to-many, and many-to-many relationships.

  • What is with () in Laravel?

    with() function is used to eager load in Laravel. Unless of using 2 or more separate queries to fetch data from the database , we can use it with() method after the first command. It provides a better user experience as we do not have to wait for a longer period of time in fetching data from the database.

  • What is difference between attach and sync in Laravel?

    The attach function only adds records to the Pivot table. The sync function replaces the current records with the new records. This is very useful for updating a model.

  • What is use of latest() in Laravel?

    latest() function in Laravel used to get latest records from database using default column created_at.

  • What is eager loading in Laravel?

    Eager loading is super simple using Laravel and basically prevents you from encountering the N+1 problem with your data. This problem is caused by making N+1 queries to the database, where N is the number of items being fetched from the database.

  • Which is better lazy loading or eager loading?

    Eager Loading. Eager loading generates all web page content as soon as possible, while lazy loading delays the display of non-essential content.

  • Does lazy load improve speed?

    Lazy-loading images and video reduces initial page load time, initial page weight, and system resource usage, all of which have positive impacts on performance.

  • How to update parent model timestamp on create or update child model?

    In Laravel a model that has a belongs to relationship to another, also has a _id field on its table that points to the id of the parent. When that model is being associated to another the updated_at timespamp naturally gets updated, as the value of the _id field changes

  • How to store timestamps() in Laravel?

    By default, those pivot tables do not contain timestamps. And Laravel does not try to fill in created _at/updated _at in this situation. You can save the timestamps automatically all you need to do is to add them into migration file, and then define relationship using ->withTimestamps();

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.