From a433ff8a9bcd88ddfe2335801a15c71b4d1a0a3a Mon Sep 17 00:00:00 2001 From: Kevin Ruscoe Date: Fri, 5 Jan 2018 16:23:47 +0000 Subject: [PATCH] Update InteractsWithPivotTable.php As with #22484 , when inserting a pivot record via `->attach()` the `InteractsWithPivotTable.php` trait doesn't check to see if the pivot table has a model representation. It' simply finds the table name and inserts a new record. Because of this, the timestamps are passed as `Carbon` instances and then turn to strings via its `toString()` method. If the consumer is using a pivot Model, it should use that models dateFormat to format by, not the `toString()` method. This simply news-up the pivot model, fetches it's dateformat and formats the `$fresh` date against it. --- .../Eloquent/Relations/Concerns/InteractsWithPivotTable.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php index 83da64e83da7..80d619447886 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php @@ -313,6 +313,11 @@ protected function baseAttachRecord($id, $timed) protected function addTimestampsToAttachment(array $record, $exists = false) { $fresh = $this->parent->freshTimestamp(); + + if ($this->using) { + $pivotModel = new $this->using; + $fresh = $fresh->format($pivotModel::getDateFormat()); + } if (! $exists && $this->hasPivotColumn($this->createdAt())) { $record[$this->createdAt()] = $fresh;