Laravel Pro Tips
#carbon #dates
π₯ #Laravel Tip: If you need to render dates and times a lot in your app, then consider making a custom Carbon class. You can then tell Laravel to always use your class via the Date facade.
Matt Kingshott
#arrays
Shoutout to my favorite array helper in Laravel π Arr::random returns one or multiple random values from an array. Unlike the native array_rand which return a random key.
Daniel Eckermann
#models #events
π₯ #Laravel Tip: At scale, calculating metrics for dashboards can take a long time, as you might have to count thousands or even millions of records. Fortunately, you can easily fix this by using model events to maintain stats. Hereβs a simple example:
#eloquent #relationships #eager loading
When eager loading relationships in Laravel, you can specify the columns you need directly in the `with` method π
#requests #authorization #gates
πΆοΈ #Laravel Tip: Cleaning up the Controller is a responsibility! I find myself moving things out of the Controller to FormRequest, Action, Services, etc. Here's how you can authorize a request inside the FormRequest & not in the Controller Action.
Bilal Haidar - Available 4 Laravel gigs
#cheatsheet #lang
πA #Laravel Lang (Translation) #Cheatsheet #PHP
Aaron Lumsden π | Indie Hacker Dev
#requests #middleware
Laravel Tip π‘ Did you know you can enrich request data using a middleware? For example, resolve the user origin country using their IP and add the country abbreviation to the request.
Philo Hermans
#encryption
Laravel Tip: If we want to put "sensitive" data in our DB then we can use Crypt Facade and handle these using accessors and mutators. #Laravel #PHP
Theodoros Kafantaris
#eloquent #relationships
An efficient way of checking if a relation exists in Laravel is to use withExists. It performs a simple existence check and returns a boolean. By default it would return a "posts_exists" value on the User. You may alias it like in the example below to make it more readable.