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

m33ch's avatar

Set/Get Date Mutator in Pivot

Hi all,

is it possible set date mutator in pivot relations? This is my tables structures :

foo :

  • id
  • title

bar :

  • id
  • title

foo_bar

  • id
  • foo_id
  • bar_id
  • start_date
  • end_date

and related models:

Foo Model

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Foo extends Model {
    public function bar()
    {
        return $this->belongsToMany('Bar 'foo_bar')->withPivot('start_date', 'end_date');
    }

    public function newPivot(\Illuminate\Database\Eloquent\Model $parent, array $attributes, $table, $exists)
    {
        return new FooBar($parent, $attributes, $table, $exists);
    }
}

Bar Model :

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Bar extends Model {
    public function foo()
    {
        return $this->belongsToMany('Foo 'foo_bar')->withPivot('start_date', 'end_date');
    }

    public function newPivot(\Illuminate\Database\Eloquent\Model $parent, array $attributes, $table, $exists)
    {
        return new FooBar($parent, $attributes, $table, $exists);
    }
}

And FooBar Pivot

<?php namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;
use Carbon\Carbon;

class FooBar extends Pivot {

    public function getStartDateAttribute($value)
    {
        if ($value)
            return Carbon::createFromFormat('Y-m-d', $value)->format('d/m/Y');
    }
    public function getEndDateAttribute($value)
    {
        if ($value)
            return Carbon::createFromFormat('Y-m-d', $value)->format('d/m/Y');
    }
    public function setStartDateAttribute($value)
    {
        $this->attributes['start_date'] = Carbon::createFromFormat('d/m/Y', $value)->toDateString();
    }
    public function setEndDateAttribute($value)
    {
        $this->attributes['end_date'] = Carbon::createFromFormat('d/m/Y', $value)->toDateString();
    }
}

Test :

$fooData1 = ['title' => 'foo test 1' ]; 
$foo1 = Foo::create($fooData1); 

$fooData2 = ['title' => 'foo test 2' ]; 
$foo2 = Foo::create($fooData2); 

$fooBar1 = [ 'start_date' => '20/03/2015', 'end_date' => '25/03/2015' ]; 
$fooBar2 = [ 'start_date' => '20/02/2015', 'end_date' => '25/02/2015' ]; 

$bar->foo()->sync( [$foo1->id => $fooBar1] ); 
$bar->foo()->sync( [$foo2->id => $fooBar2] );

Set date mutator not work!

Thanks!

0 likes
4 replies
JarekTkaczyk's avatar
Level 53

@m33ch Yes, it is possible to use mutators in pivot model but it will not affect relation methods, ie. sync, attach etc. since pivot model is never touched during these calls.

1 like
m33ch's avatar

@JarekTkaczyk thank for you reply! So you suggest me to edit date fields in (change format with carbon) in controller? Or is there a method to call set/get mutator in pivot model?

Thank you very much!

JarekTkaczyk's avatar

@m33ch If you do it in the controller, then yes - prepare the dates there. I would suggest a repository or a service that will handle this thing for you so you don't have to take care of this task in the controller, since it is not supposed to do this.

m33ch's avatar

actually i use a repository. Best regards!

Please or to participate in this conversation.