Skip to content

Commit 1cd16ac

Browse files
authored
Use model created_at for form submission date() (#306)
* Fallback to created_at date for formsubmissions * Don't fallback to data['date'] * Add test coverage * 🍺 * Update test to use attribute
1 parent cd017fd commit 1cd16ac

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Forms/Submission.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function fromModel(Model $model)
2222
{
2323
return (new static())
2424
->id($model->id)
25-
->date($model->data['date'] ?? Carbon::now())
25+
->date($model->created_at ?? Carbon::now())
2626
->data(Arr::except($model->data, 'date'))
2727
->model($model);
2828
}

tests/Forms/FormSubmissionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\Attributes\Test;
77
use Statamic\Eloquent\Forms\FormModel;
88
use Statamic\Eloquent\Forms\SubmissionModel;
9+
use Statamic\Facades;
910
use Tests\TestCase;
1011

1112
class FormSubmissionTest extends TestCase
@@ -81,4 +82,23 @@ public function it_should_not_overwrite_submissions()
8182

8283
$this->assertCount(2, SubmissionModel::all());
8384
}
85+
86+
#[Test]
87+
public function it_should_not_save_date_in_data()
88+
{
89+
$form = tap(Facades\Form::make('test')->title('Test'))
90+
->save();
91+
92+
$submission = tap($form->makeSubmission([
93+
'name' => 'John Doe',
94+
]))->save();
95+
96+
$this->assertInstanceOf(Carbon::class, $submission->date());
97+
$this->assertArrayNotHasKey('date', $submission->model()->data);
98+
99+
$fresh = \Statamic\Eloquent\Forms\Submission::fromModel($submission->model()->fresh());
100+
101+
$this->assertInstanceOf(Carbon::class, $fresh->date());
102+
$this->assertSame($fresh->date()->format('u'), $submission->date()->format('u'));
103+
}
84104
}

0 commit comments

Comments
 (0)