Skip to content

DOCSP-37112: Casts() method info #2901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/eloquent-models/model-class.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ type, to the Laravel ``datetime`` type.
:emphasize-lines: 9-11
:dedent:

.. tip:: Casts in Laravel 11

In Laravel 11, you can define a ``casts()`` method to specify data type conversions
instead of using the ``$casts`` attribute. The following code performs the same
conversion as the preceding example by using a ``casts()`` method:

.. code-block:: php

protected function casts(): array
{
return [
'discovery_dt' => 'datetime',
];
}

To learn more, see `Attribute Casting <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-mutators#attribute-casting>`__
in the Laravel documentation.

This conversion lets you use the PHP `DateTime <https://www.php.net/manual/en/class.datetime.php>`__
or the `Carbon class <https://carbon.nesbot.com/docs/>`__ to work with dates
in this field. The following example shows a Laravel query that uses the
Expand Down