Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit d5d7949

Browse files
committed
Docs
1 parent a008d1f commit d5d7949

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,38 @@ Instead of setting a default value, you can also pass in a target, like an Eloqu
159159

160160
In the example above, where `$video` is an Eloquent model, the default value will be `$video->description`.
161161

162+
#### Date Casting
163+
164+
If you use Eloquent's [Date Casting](https://laravel.com/docs/8.x/eloquent-mutators#date-casting) feature, you can use the date attributes in your forms by setting the `use_eloquent_date_casting` configuration key to `true`. For compatibility reasons, this is disabled by default.
165+
166+
```php
167+
return [
168+
'use_eloquent_date_casting' => true,
169+
];
170+
```
171+
172+
You can either use the `dates` property or the `casts` property in your Eloquent model to specify date attributes:
173+
174+
```php
175+
class ActivityModel extends Model
176+
{
177+
public $dates = ['finished_at'];
178+
179+
public $casts = [
180+
'started_at' => 'date',
181+
'failed_at' => 'datetime',
182+
'completed_at' => 'date:d-m-Y',
183+
'skipped_at' => 'datetime:Y-m-d H:i',
184+
];
185+
}
186+
```
187+
188+
```blade
189+
<x-form-input name="completed_at" :bind="$activity" />
190+
```
191+
192+
In the example above, the default value will be formatted like `31-10-2021`.
193+
162194
#### Binding a target to multiple elements
163195

164196
You can also bind a target by using the `@bind` directive. This will bind the target to all elements until the `@endbind` directive.

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
/** tailwind | tailwind-2 | bootstrap-4 | bootstrap-5 */
99
'framework' => 'tailwind',
1010

11+
'use_eloquent_date_casting' => false,
12+
1113
'components' => [
1214
'form' => [
1315
'view' => 'form-components::{framework}.form',

src/Components/HandlesBoundValues.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
88
use Illuminate\Database\Eloquent\Relations\MorphMany;
99
use Illuminate\Support\Carbon;
10+
use Illuminate\Support\Str;
1011
use ProtoneMedia\LaravelFormComponents\FormDataBinder;
1112

1213
trait HandlesBoundValues
@@ -77,6 +78,10 @@ private function getBoundValue($bind, string $name)
7778
*/
7879
private function formatDateTime(Model $model, string $key, DateTimeInterface $date)
7980
{
81+
if (!config('form-components.use_eloquent_date_casting')) {
82+
return $date;
83+
}
84+
8085
$cast = $model->getCasts()[$key] ?? null;
8186

8287
if (!$cast || $cast === 'date' || $cast === 'datetime') {
@@ -98,7 +103,12 @@ private function formatDateTime(Model $model, string $key, DateTimeInterface $da
98103
*/
99104
protected function isCustomDateTimeCast($cast)
100105
{
101-
return strncmp($cast, 'date:', 5) === 0 || strncmp($cast, 'datetime:', 9) === 0;
106+
return Str::startsWith($cast, [
107+
'date:',
108+
'datetime:',
109+
'immutable_date:',
110+
'immutable_datetime:',
111+
]);
102112
}
103113

104114
/**

tests/Feature/ActivityModel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class ActivityModel extends Model
1111
'date_c' => 'datetime',
1212
'date_d' => 'date:Y',
1313
'date_e' => 'datetime:Y-m',
14+
'date_f' => 'immutable_date:Y',
15+
'date_g' => 'immutable_datetime:Y-m',
1416
];
1517

1618
public $dates = ['date_a'];

tests/Feature/DateTimeCastTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class DateTimeCastTest extends TestCase
1111
/** @test */
1212
public function it_supports_date_casts_and_custom_casts()
1313
{
14+
config(['form-components.use_eloquent_date_casting' => true]);
15+
1416
$this->registerTestRoute('date-time-casts');
1517

1618
Carbon::setTestNow(Carbon::parse('2021-11-01 12:00:00'));
@@ -23,13 +25,17 @@ public function it_supports_date_casts_and_custom_casts()
2325
'date_c' => now(),
2426
'date_d' => now(),
2527
'date_e' => now(),
28+
'date_f' => now(),
29+
'date_g' => now(),
2630
]));
2731

2832
$this->visit('/date-time-casts')
2933
->seeElement('input[name="date_a"][value="2021-11-01T12:00:00.000000Z"]')
3034
->seeElement('input[name="date_b"][value="2021-11-01T00:00:00.000000Z"]')
3135
->seeElement('input[name="date_c"][value="2021-11-01T12:00:00.000000Z"]')
3236
->seeElement('input[name="date_d"][value="2021"]')
33-
->seeElement('input[name="date_e"][value="2021-11"]');
37+
->seeElement('input[name="date_e"][value="2021-11"]')
38+
->seeElement('input[name="date_f"][value="2021"]')
39+
->seeElement('input[name="date_g"][value="2021-11"]');
3440
}
3541
}

tests/Feature/views/date-time-casts.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<x-form-input name="date_c" />
66
<x-form-input name="date_d" />
77
<x-form-input name="date_e" />
8+
<x-form-input name="date_f" />
9+
<x-form-input name="date_g" />
810

911
<x-form-submit />
1012
@endbind

0 commit comments

Comments
 (0)