Laravel Pro Tips
🔥 #Laravel Tip: Eloquent offers many undocumented methods that can help to make your code easier to read. For example, the whereBelongsTo method simplifies finding resources that belong to a model instance.
#Laravel Migration Tip 🔥
You can use the `from` method to set the starting value of an auto-incrementing field.
#Laravel Validation Tip 🔥
You can use a closure instead of a rule object if you only need a custom rule once throughout your application.
The closure receives the attribute's name, the attribute's value, and a `fail` callback that should be called if validation fails.
🔥 #Laravel Tip: Using custom error messages for failed request validation is straight out of the docs, but it’s amazing how rarely it gets used. Make your apps better and improve their UX by giving users more guidance in solving validation errors.
🔥 #Laravel Tip: Macros provide a way to add missing functionality to many parts of the framework, such as the query builder. These macros can be "user-facing", but they don't have to be. Sometimes they can be used just to make your tests simpler (or to aid in debugging).
💡 You can define HTTP macro to configure common request paths and headers when interacting with services throughout your application.
#laravel #laravelphp #laraveltips #laraveltricks #laravelupdates #laravelfeatures
Laravel Updates | Tips and Tricks
TIL, in Laravel you can display debug information about the current query with dump() and dd() 🤙
This allows you to see the underlying query and bindings.
🔥 #Laravel Tip: Sometimes you want to validate part of the request's data, but you don't actually need the data (and must make a point of filtering it out) e.g. a bool confirming the user accepts terms and conditions. In these situations, use the 'exclude' validation rule.
🔥 #Laravel Tip: Instructing the database to delete a huge number of records is likely to make it fall over (esp. when using cascade deletion). However, you can avoid this by staggering the process into batches and adding pauses to allow the database time to catch up.