Laravel 7 has released. And like any proper major release, it's packed to the rafters with new and exciting features. It's time to take a look at three of my favourite new features, and, if we're lucky, there might be a bonus feature at the end.

You’ll need some existing knowledge of PHP and a little Laravel knowledge to follow along. You can find the sample code used in this article on GitHub.

Custom Casting with Eloquent

Eloquent, Laravel's object relationship mapper is a useful tool for moving data between our database and our code. We could do some sensible type conversions using Eloquent’s Mutators already. For example, add any field to the $dates property of our model and it is automatically converted into a Carbon date object for us.

Custom Casts take this a step further by helping us to decouple how our data looks in our database and our application. Often we'll want to store our data one way, and interact with it in our application another. Custom casts allow us to automatically convert data between the database state and the application state on read and write.

Let's assume we have a transactions table described by this migration:

database/migrations/create_transactions_table.php