Skip to content

Commit ffecbdc

Browse files
authored
DOCSP-37112: Casts() method info (#2901)
Adds an admonition about Laravel 11 casts changes
1 parent 4abdb8a commit ffecbdc

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/eloquent-models/model-class.txt

+18
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,24 @@ type, to the Laravel ``datetime`` type.
194194
:emphasize-lines: 9-11
195195
:dedent:
196196

197+
.. tip:: Casts in Laravel 11
198+
199+
In Laravel 11, you can define a ``casts()`` method to specify data type conversions
200+
instead of using the ``$casts`` attribute. The following code performs the same
201+
conversion as the preceding example by using a ``casts()`` method:
202+
203+
.. code-block:: php
204+
205+
protected function casts(): array
206+
{
207+
return [
208+
'discovery_dt' => 'datetime',
209+
];
210+
}
211+
212+
To learn more, see `Attribute Casting <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-mutators#attribute-casting>`__
213+
in the Laravel documentation.
214+
197215
This conversion lets you use the PHP `DateTime <https://www.php.net/manual/en/class.datetime.php>`__
198216
or the `Carbon class <https://carbon.nesbot.com/docs/>`__ to work with dates
199217
in this field. The following example shows a Laravel query that uses the

0 commit comments

Comments
 (0)