Laravel Pro Tips
#eloquent #eager loading #n+1
๐ฅ #Laravel Tip: Eager loading a modelโs relationships is a great way to improve performance / avoid the N+1 problem. However, you can go furtherโฆ with constrained eager loading you can tell Laravel to only load the related records you actually need.
Matt Kingshott
#eloquent #relationships #subqueries
๐ฅ #Laravel If you're only using one record from your many-to-many relationships, you can use subqueries instead of loading all the records. For example, if I only need to know the title of the most viewed post from a user. This can reduce your memory usage significantly.
Cosme Escobedo
#validation #models
Laravel Tip: While defining a Unique Validation Rule, you can also pass the Model Name instead of passing the Table Name. ๐ฅ
The Laravel Dev
#events #authentication
๐ฅ #Laravel Tip: If your app needs to record logins and logouts for security purposes (auditing), then you should definitely make use of the auth events, which allow you to easily listen for them.
#models #events
๐ถ๏ธ #Laravel Tip: Laravel offers multiple ways to allow us to register & listen to Model Events. You can register them via: 1โฃ Model itself 2โฃ App Service Provider 3โฃ Custom Service Provider Where else? Can you name some? #PHP #100Devs
Bilal Haidar
#validation #requests
๐ฅ #Laravel Tip: Sometimes the default error messages returned when data validation fails are not always helpful in solving the problem. Fortunately, with form requests, we can supply our own error messages to make things clearer to the user.
#cache #artisan
Just found out there's a single artisan command to clear out all Laravel caches, much less typing!
Andrew Schmelyun
#routes #models
๐ฅ #Laravel Tip: Sometimes, you may need to use custom logic when you want to resolve a model within a route. Laravel supports this via explicit route model binding. Check it outโฆ
#casts #mutators
๐ถ๏ธ #Laravel Tip: When using an ๐ฎ๐ฐ๐ฐ๐ฒ๐๐๐ผ๐ฟ remember to always cast the data to it's real type, even though you're casting it inside $๐ฐ๐ฎ๐๐๐. Both casts and accessors work on the actual data ๐ค #PHP #100Devs