πŸ”₯ Weekly thread #01 of 2022

πŸ”₯ Latest code tips for Laravel developers

First week's special: news from the past 3 weeks 🧡

Custom collections pair well with higher order proxies

14

πŸ”₯ Custom collections pair well with higher order proxies

Custom collections pair well with higher order proxies

πŸ”₯ Custom collections pair very well with higher order proxies

When you have some repetitive low level logic that you'd like to abstract into more readable methods that follow your business terminology, custom collections can be a great choice

PHP 8.1 enums can be used as model casts

13

πŸ”₯ PHP 8.1 enums can be used as model casts

PHP 8.1 enums can be used as model casts

πŸ”₯ Did you know that @laravelphp can cast model columns into instances an enum in PHP 8.1?

The example below will create an instance of OrderStatus based on variant value. If the status column has the value shipped, it will return the OrderStatus::Shipped variant.

You can supply default values for route parameters

10

πŸ”₯ You can supply default values for route parameters

You can supply default values for route parameters

πŸ”₯ Did you know that you can supply default values for route parameters in @laravelphp?

In the screenshot below, we use the current user's account as the default value for {account}. This means we don't have to provide it when generating the users.index route.

You can use generic class-string annotations to hint that an instance of the passed class is returned

9

πŸ”₯ You can use generic class-string annotations to hint that an instance of the passed class is returned

You can use generic class-string annotations to hint that an instance of the passed class is returned

This is how you can hint that a function returns an instance of the class of which you passed the class name.

Cool stuff!

Hat tip to @enunomaduro

https://github.com/spatie/laravel-route-discovery/blob/7fa88922aa693b03b4763eda870867075ab44999/src/PendingRoutes/PendingRouteAction.php#L100-L107

Use partitions for deleting mass amounts of data

8

πŸ”₯ Use partitions for deleting mass amounts of data

Use partitions for deleting mass amounts of data

⚑ ️Database Tip

Some applications want to periodically deleted old historic data (or have to because of regulations). Deleting billions of rows is a very slow task which could take several minutes and may take your application down. Use partitions for efficient deleting!

You can use `onerror` on the image tag

6

πŸ”₯ You can use `onerror` on the image tag

You can use `onerror` on the image tag

😲 TIL: You can hide an image when it is broken so you don't get that ugly broken image icon from the browser.

Use skip() when writing tests ahead of time

4

πŸ”₯ Use skip() when writing tests ahead of time

Use skip() when writing tests ahead of time

🌢️ #Laravel #pestphp tip

If you like to plan your tests ahead of time and implement them one by one, you can always make use of a handy method skip() to tell the test runner to ignore those empty tests.

skip() accepts a callback too!

Number markdown lists with `1.` rather than the actual number

3

πŸ”₯ Number markdown lists with `1.` rather than the actual number

Number markdown lists with `1.` rather than the actual number

πŸ’‘ A little thing I always like to do. Use only "1." for numbered lists in markdown. The list is rendered the same, but adding an item doesn't require you to renumber the other items 🀘

You can use first class callables for routes in PHP 8.1

2

πŸ”₯ You can use first class callables for routes in PHP 8.1

You can use first class callables for routes in PHP 8.1

πŸ”₯ You can use first class callables for routes in PHP 8.1

The caveat is that the methods need to be static because there's no syntax for referencing instance methods on classes

I don't use constructor DI in my apps, so static methods are fine despite feeling a bit non-standard

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