{ Laravel snippets }

Gurmandeep Singh

Gurmandeep Singh @iamgurmandeep

#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. https://t.co/Bl0EkmArzu

Matt Kingshott 🏝

Matt Kingshott 🏝 @mattkingshott

πŸ”₯ #Laravel Tip: A common pattern in websites that offer searchable content, is to suggest content when the user's query returns no results. Laravel can help with this via the 'existsOr' method. Here's an example that finds an alternative vacation destination. https://t.co/dvYvcJIuNr

Matt Kingshott 🏝

Matt Kingshott 🏝 @mattkingshott

πŸ”₯ #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. https://t.co/xkv0If70ac

Matt Kingshott 🏝

Matt Kingshott 🏝 @mattkingshott

πŸ”₯ #Laravel Tip: A common task when performing an authorisation check, is to verify if a particular model (such as the authenticated user) owns another model (e.g. a blog post). Since you're likely to do this a lot, consider adding an 'Ownership' trait to your models: https://t.co/U5ZPfAm9PS

Matt Kingshott 🏝

Matt Kingshott 🏝 @mattkingshott

πŸ”₯ #Laravel Tip: When storing database notifications, you may want to avoid adding duplicates, so that a user's β€œfeed” only contains unique content. You can do this by adding a hash of the content and then enforcing a unique constraint on the table. https://t.co/ahbbMYxsA8

Ryan Chandler

Ryan Chandler @ryangjchandler

⚑️ Did you know that Laravel now has first-party directives for conditionally outputting disabled, checked and selected attributes on HTML elements? Much nicer than if directives or ternaries. https://t.co/5vFsO9JER2

Matt Kingshott 🏝

Matt Kingshott 🏝 @mattkingshott

πŸ”₯ #Laravel Tip: Rate-limiting is not only for throttling entire requests. You can also use it to throttle methods or parts of your code, which can be really useful when you need to protect things against spikes e.g. the DB. https://t.co/1pN04w9WXW

Chris⚑Arter 🀘

Chris⚑Arter 🀘 @ChrisArter

TIL about secret columns in a new version of MySQL 8. What are they? They are columns that do not show up in a SELECT * statement. They must be explicitly declared. (Thanks to @mmartin_joo πŸ₯³) Here's an example in Laravel: https://t.co/NN4ftjN1O7

Dean Morgan

Dean Morgan @deanzod

Quick #laravel tip. Use whereIntegerInRaw() instead of whereIn() if you are querying against a large array of integers. One of my test queries went from 25 seconds to 200ms. #laraveltips

Andrew Schmelyun

Andrew Schmelyun @aschmelyun

Recently learned about fluent JSON testing in Laravel, basically being able to easily test an expected API's response down to the attributes + their values! https://t.co/eCnSRClWSb

Philo Hermans

Philo Hermans @Philo01

Laravel productivity tip! Add this little function to your bash profile to quickly execute anything with the Tinker command and get the results instantly. https://t.co/qIB9pQCTWt

Michael Dyrynda πŸ’‰πŸ’‰

Michael Dyrynda πŸ’‰πŸ’‰ @michaeldyrynda

πŸ”₯ An important consideration when working with @laravelphp's environment and config tooling that bit me. Keys that exist in your `.env` file with no value will not fallback to the `env()` helper's default value. To achieve the desired outcome, use null coalescing instead. https://t.co/qptuGEPzMN

Jonathan Reinink

Jonathan Reinink @reinink

πŸ”₯ Ever wondered how to set active states on links in an @inertiajs app? It's actually really easy to do! πŸ’β€β™‚οΈ Simply do string comparisons against the `page.url` or `page.component` properties. You can do exact matches, or "startsWith" checks, or even regular expressions! https://t.co/Gc3I917bVJ