99

I'm creating a row in pivot table using the following attach statement.

$music = Music::find(1);
$music->users()->attach(1);

This inserting a row in pivot table, but it is not updating the timestamp. The timestamp remains as 0000-00-00 00:00:00.

Is there any way to update the timestamps in pivot table?

starball
  • 20,030
  • 7
  • 43
  • 238
Sriraman
  • 7,658
  • 4
  • 40
  • 60

1 Answers1

262

If you want your pivot table to have automatically maintained created_at and updated_at timestamps, use the withTimestamps() method on the relationship definition.

return $this->belongsToMany('Role')->withTimestamps();
Lukáš Irsák
  • 1,092
  • 1
  • 14
  • 23
marcanuy
  • 23,118
  • 9
  • 64
  • 113