How to update timestamp in pivot table in Laravel

August 7, 2020
zemna@zemna.net
Category:

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();
  }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram