Laravel: Many-to-many relations with Timestamps

Published March 30, 2020 • 1 min read

Imagine you have a Many-to-Many relationship between the model Post and Tag. Now in the pivot table you want the timestamps to be maintained automatically as well. 

All you have to do is to use the method withTimestamps in the relationship definition. 

Example:

public function tags() 
{
  return $this->belongsToMany('App\Post')->withTimestamps(); 
}
laravel

Join my mailing list