Laravel Pro Tips
#eloquent #models #relationships
🔥 #Laravel Tip: Let’s start the week with a simple one… when making changes to a model and its relations, you don’t need to call ‘save()’ on each of them. Instead, just call ‘push()’ on the main model and it will recursively save any changes.
Matt Kingshott
#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
#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
#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.
#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 - Available 4 Laravel gigs
#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.
#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
#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