Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamps are not updating while attaching data in pivot table

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 0000-00-00 00:00:00

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

Thanks in advance,

like image 687
Sriraman Avatar asked Sep 08 '14 14:09

Sriraman


1 Answers

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();
  • For Laravel 4.2: Working With Pivot Tables
  • Laravel 5.0: Working With Pivot Tables
  • Laravel 5.1: Retrieving Intermediate Table Columns
  • Laravel 5.2: Filtering Relationships Via Intermediate Table Columns
  • Laravel 5.3: Filtering Relationships Via Intermediate Table Columns
  • Laravel 5.4: Retrieving Intermediate Table Columns
  • Laravel 5.5: Retrieving Intermediate Table Columns
  • Laravel 5.6: Retrieving Intermediate Table Columns
  • Laravel 5.7: Retrieving Intermediate Table Columns
  • Laravel 5.8: Retrieving Intermediate Table Columns
like image 200
marcanuy Avatar answered Oct 06 '22 20:10

marcanuy