๐Ÿ”ฅ Weekly thread #11 of 2022

๐Ÿ”ฅ This week's code tips for Laravel developers

A weekly thread ๐Ÿงต

You can use $this->mock in Pest tests

8

๐Ÿ”ฅ You can use $this->mock in Pest tests

You can use $this->mock in Pest tests

Within your Pest / PHPUnit tests, you may use @laravelphp's "mock" method to instruct the container to use your mocked instance of the object instead of constructing the object itself. โœ…

PHP traits can contain abstract methods

6

๐Ÿ”ฅ PHP traits can contain abstract methods

PHP traits can contain abstract methods

๐Ÿ’กDid you know you can write abstract methods in a trait?

This way you don't have to make assumptions about the models where the trait is used.

But instead, you have a clear contract:

You can define casts for specific Eloquent queries

4

๐Ÿ”ฅ You can define casts for specific Eloquent queries

You can define casts for specific Eloquent queries

๐Ÿ”ฅ #Laravel Query Time Casting

We all know you can add casts to your eloquent models, but did you know you can add casts to a specific query?

You can validate data at the database level

3

๐Ÿ”ฅ You can validate data at the database level

You can validate data at the database level

โšก๏ธ Database Tip

Your application will have bugs and any data stored may be corrupt forever. But with constraints on the data model, rules would have prevented saving incorrect data in the first place! Use them as a last frontier to make sure you always have valid data.

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