Laravel Custom Cast

Category: Laravel

One of the new features in Laravel 7 is the creation of custom eloquent casts. These provide the majority of use cases for allowing you to change the value of the attribute on the way into the database and on the way out.Alternatively, if you want to change the value of the attributes you can use mutator getter and setter methods. When eloquent attempts to fill the model it will use these mutator attributes to change the value of the attributes on the way in and out of the database. The downside of this approach is that it populates the model class which can grow larger and harder to manage.

Set is used when saving the model in the database we can take the date value and convert it to UTC.

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Community Partners