How to update timestamp in pivot table in Laravel

Author: zemna
Date: April 17, 2021
Share this:

When insert a new row to pivot table, we can use attach() method like this:

$company = Company::find(1);
$company->users()->attach(1);

But this will not update created_at and updated_at timestamps column in pivot table.

To update timestamps, use withTimestamps() method on the relationship method.

class Company extends Model
{
public function users()
{
return $this->belongsToMany(User::class)->withTimestamps();
}
}
Share this:

Leave a Reply

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

Let's connect and create
something awesome together!
2023 - Copyright, All Rights Reserved, Made by ZEMNA.NET with ❤️
crossmenu linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram