Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fakeheal's avatar

Eloquent - timestamps in pivot tables

I have a relation between two tables - Many To Many.

I use the sync() method to save the related models:

public function tags()
{
    return $this->belongsToMany('Tag')->withTimestamps();
}

And this way I can set the timestamp fields - created_at and updated_at, but how can I override the format of those dates.

I have overriden the format for models using the following:

protected function getDateFormat()
{
    return 'U';
}

But it doesn't seem to work when using sync(). All records in the table are saved with the default date format.

0 likes
4 replies
JarekTkaczyk's avatar
Level 53

@fakeheal You can't do that.

Eloquent BelongsToMany doesn't let you override format for the pivot table, since behind the scenes it relies on the base Grammar's getDateFormat method, rather than model's getDateFormat. I would say it is inconsistency that needs to be addressed, nonetheless you just can't do it.

2 likes
rmff's avatar

A bit off topic.

I was looking for, how update timestamps only on pivot and didn't found anything useful... after a lot of search I realize that was just touch pivot relation using :

$model->pivot->touch() 

This will update pivot updated_at but don't touch on model's timestamps.

ketchol's avatar

This question is a bit old. But I've bumped into this issue while working with Laravel 5.6.

In 5.6 you may specify a Model class for Pivot table https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

when defining relationship

return $this->belongsToMany('TableModelClassName')->using('PivotTableModelClassName')->withTimestamps();

And in Pivot Table Model class, you may specify its date format in the following attribute.

protected $dateFormat = 'The format you need';
2 likes
nolros's avatar

@JarekTkaczyk how are doing sir ? Been a long time. Couldn't you write something like this ? Rough concept have not run or tested it, but wonder if this could be made to work?

class AppServiceProvider
{
    public function boot()
    {

         /**
         * we pass it instance or just date and return new date
         */ 
         Carbon::macro('dateFormater', function ($instance) {
            $dt = Carbon::parse($instance->created_at); 
            
            if (!isset($self) && isset($this)) {
                $self = $this;
            }
  
            return  $dt->format('m-d');
        });


        /**
         * we pass builder callback
         */ 
        Builder::macro('myAppTags', function($callback) {
   
            if (count(call_user_func($callback->bindTo($this)))) {
                foreach ($variable as $key => $value) {
                    variable[$key]['new_date'] = Carbon::macro('dateFormater')
                }
            }
            return $this;
        });

       /**
         * Start here. 
         */ 
        SomeModel::myAppTags(function () {
            return $this->belongsToMany('Tag')->withTimestamps();
        });

    }
}

Please or to participate in this conversation.