🔥 Weekly thread #20 of 2022

🔥 This week's code tips for Laravel developers

A weekly thread 🧵

Consider creating helpers for model factories

4

🔥 Consider creating helpers for model factories

Consider creating helpers for model factories

If your app has only a few models, consider creating helper functions for model factories

Helpers have perfect IDE support and can make your tests a lot cleaner

Pairs especially well with @pestphp 💅

orderByRaw() is very useful for complex sorting in Eloquent

3

🔥 orderByRaw() is very useful for complex sorting in Eloquent

orderByRaw() is very useful for complex sorting in Eloquent

🔥 #Laravel Tip: Make use of ‘orderByRaw’ when you need to sort records in a more complex manner than simply A-Z or Z-A. Here’s an example where I sort ‘roles’ based on their level of importance (rather than their alphabetical order).

Partial indexes are useful for enforcing rules that only apply conditionally

2

🔥 Partial indexes are useful for enforcing rules that only apply conditionally

Partial indexes are useful for enforcing rules that only apply conditionally

⚡️ Database Tip

Sometimes you want to make columns unique but you can't because e.g. there are still soft-deleted rows in the table witht the same value. By only including relevant rows (partial index) you can still enforce uniqueness, and you can even simulate it for MySQL!

We're writing a book, you can get it for free here.