Skip to content

Commit a433ff8

Browse files
authored
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.
1 parent 61f8477 commit a433ff8

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php

+5
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ protected function baseAttachRecord($id, $timed)
313313
protected function addTimestampsToAttachment(array $record, $exists = false)
314314
{
315315
$fresh = $this->parent->freshTimestamp();
316+
317+
if ($this->using) {
318+
$pivotModel = new $this->using;
319+
$fresh = $fresh->format($pivotModel::getDateFormat());
320+
}
316321

317322
if (! $exists && $this->hasPivotColumn($this->createdAt())) {
318323
$record[$this->createdAt()] = $fresh;

0 commit comments

Comments
 (0)