Back

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.

image

67

0 / 1000