Using custom casts is an incredibly powerful tool and if this is the first time looking at them I really suggest going through the documentation for them. We do this with the following command:Įnter fullscreen mode Exit fullscreen modeĮqually if we wished to we could potentially write a more advanced custom cast like our money example which would break the address data into multiple columns should we want to be able to apply database indexes for particular fields like the postcode. Much like the other two examples, we’re going to install this library to use a class from this package to represent the location. Google Maps and other equivalent services.Ī useful library is Geocoder PHP which is designed to work with a number of services for looking up addresses or finding coordinates for an address. Equally it can be difficult to fetch these addresses from multiple sources e.g. The idea of storing a location in a database is pretty universal, this might be for addresses across the world but could also be for latitude and longitude coordinates. This is ultimately something you should consider when creating custom casts, will you need to query that data at a database level and how will you do it when you need to? For instance you might want to query by the days column and ultimately find that your database doesn’t know how to process the format whereas adding a simple integer as days to a timestamp is relatively easy. 1h30m and you would be right in that we could save ourselves some trouble but this might become an issue when you start to run queries in the database. As anyone who’s worked with DateInterval before, you probably know that there’s a format you can use e.g. To demo this we’re going to create an Order class that will use all of these casts, we’ll create it with the following command:Ī question you might have about this is why not just store the raw format. These 3 challenges is, storing a monetary value, a location (including address) and a date/time interval. Instead with custom casts it’s very easy to make reusable logic now across projects and doesn’t have this issue.įor the sake the article we’re going to talk about real world examples of different times a cast could come in hand. There was also problematic behaviour with mutations of objects and array on a model’s attribute requiring the whole thing to be set again. We could get around this with attribute accessors and mutators but it often meant lots of clumsy logic loaded into the model and defined for each attribute using this. Often when developing with Laravel’s models it becomes difficult to store what you want with the basic array or object casts as this often meant what you actually wanted was a class instance instead with it’s own properties and methods to manipulate the attribute. Custom casts have been in Laravel since version 7 and it’s one of the best features we’ve had in years (it had been requested for a long time).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |