about uscontact

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 *

ZEMNA.NET

Professional software development agency estalished in Indonesia. We offers website setup, web application development, fulfillment service, and etc.
Follow us
Follow us to get latest news and tutorials.
2022 - Copyright, All Rights Reserved, Made by ZEMNA.NET with ❤️
magnifiercrossmenu