Hey Laravel Developers
You must be aware of the Prunable trait that comes with Laravel.
This trait allows us to configure periodic deletions of unnecessary models from our table.
But did you know that you can add a pruning() method to your model class, which is automatically triggered when the model is deleted during the pruning process?
This can be useful when you need to perform additional operations along with the deletion.
For example, you might want to make an API call to a third-party service to delete the resource there as well.
Hey Laravel Developers
I want to introduce you to the Dumpable trait that comes with Laravel.
This trait can be added to any of your custom classes, allowing you to chain the dump() and dd() methods on objects of that class.
By default, when you call dd() or dump() on an object, it prints the current object.
You can also pass additional variables to the chained dump() and dd() calls to print them as well.
This same trait is internally used by Laravel, which is why the Request, HTTP and other classes exhibit similar behaviour.
Hey Laravel Developers
Starting with Laravel v11.39.1, a new method called incrementOrCreate() is available on the Eloquent builder class.
As the name suggests, this method either increments the specified column if a record already exists or creates a new record with the increment counter set to 1.
This method works similarly to updateOrCreate or firstOrCreate under the hood.
A practical use case for this method is tracking and incrementing total product views in a separate table on an e-commerce website, as shown below.
Hey Laravel Developers,
When working with image uploads in your Laravel application, it's important to validate the width and height of the uploaded image.
For instance, if you're allowing users to upload their avatars, you may want to ensure that the dimensions are consistent for all users.
No worries! Laravel's Rule class makes it easy to validate the width and height of an image with an expressive syntax.
Here’s an example code snippet:
Hey Laravel Developers
Starting with Laravel v11.38.0, we can now customize the validation rules for "Email" similarly to how we have them with the "Password" rule using fluent method chaining.
This approach of chaining on methods over using strings leads to less typos and also brings more opportunity for customization in the future like configuration of defaults.
Here is an example of before and after you use the fluent Email class for validation.