πŸ”₯ Weekly thread #49 of 2021

πŸ”₯ This week's code tips for Laravel developers

🧡 A weekly thread

You can use $request->whenFilled() to execute some logic only when a specific value is part of the request data

7

πŸ”₯ You can use $request->whenFilled() to execute some logic only when a specific value is part of the request data

You can use $request->whenFilled() to execute some logic only when a specific value is part of the request data

πŸ”₯Another amazing #Laravel feature!πŸ”₯

We often write if statements to check if a value is present on a request or not.

You can simplify it with the whenFilled() helper.

πŸ‘‹If you find this useful, consider clicking the button that says Retweet. It's a very cool button!

Writing simple "page can be visited" tests is a great way to get started with testing

6

πŸ”₯ Writing simple "page can be visited" tests is a great way to get started with testing

Writing simple "page can be visited" tests is a great way to get started with testing

Does your @laravelphp application have 0 tests? Here is one test you can easily add to get started. It's probably the most important test β€” in web projects β€” and it has an enormous value.

βœ“ Ensures your application boots. βœ… βœ“ Ensures the home page can be loaded. πŸ’¨

You can calculate multiple aggregates in a single database query

3

πŸ”₯ You can calculate multiple aggregates in a single database query

You can calculate multiple aggregates in a single database query

⚑️ Database Tip

You don't have to execute multiple queries to calculate different aggregates. With the filter clause you can narrow the rows which should be included for the calculation. So you may need to scan the table only once, the performance impact can be massive πŸ”₯

Use typehints wherever possible to improve code safety and IDE support

2

πŸ”₯ Use typehints wherever possible to improve code safety and IDE support

Use typehints wherever possible to improve code safety and IDE support

πŸ”₯ Simple typehints and annotations go a long way

Even without any docblocks, this code is perfectly understandable by both developers and IDEs

Writing code like this: πŸ‘‰ Prevents bugs using type safety πŸ‘‰ Makes the code self-documenting πŸ‘‰ Takes only a few seconds to implement

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