Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jcodes's avatar

Calculate a value based on Auto Increment ID & other field before saving

Hello, the use case is where I want to generate a calculated value using the auto-increment ID and another column to set a column value at the time of record insert.

Here is the code I am using:

public static function boot()
{
    parent::boot();
    Order::saving(function (Order $order) {
         $order->total = $order->id * $order->quantity;
    });
}

(above is a dummy example, just to iterate the kind of results required).

The above code doesn't work with ID field since this is the default auto increment field, but it works fine with the other columns.

Can anyone suggest a solution here?

0 likes
7 replies
tangtang's avatar

@jcodes

why you do this $order->id * $order->quantity code? isn't it mean you will have a not very suitable result. assume there an id increment 50 * 2, isn't the total order is 100. user just want order 2 not 100

jcodes's avatar

@tangtang It is the dummy use case. The actual case is different but the idea is to achieve the result here

tangtang's avatar

@jcodes

nah, if that the case just change the saving with creating

using the creating event, you ensure that the total attribute is calculated using the auto-increment ID and the quantity attribute just before the record is saved

public static function boot()
{
    parent::boot();
    Order::creating(function (Order $order) {
         $order->total = $order->id * $order->quantity;
    });
}
jcodes's avatar

@tangtang Tried this already, getting the following error:

Integrity constraint violation: 1048 Column 'unique_id' cannot be null

Should the column be set to nullable to achieve this?

tangtang's avatar

@jcodes

the unique_id must not nullable, but you can try to override the save method in the Order model. if the model doesn't exist (i.e., it's a new record being created), calculate the total value based on the quantity and the auto-incremented id. then, call the parent save method to perform the actual record insertion.

this is the reference

class Order extends Model
{
    public function save(array $options = [])
    {
        if (!$this->exists) {
            $this->total = $this->quantity * $this->id;
        }

        parent::save($options);
    }
}

and call the insert/save data in controller like this

$order = new Order();
$order->quantity = 10; // set the quantity value
// the 'total' attribute will be calculated automatically based on the 'id' and 'quantity' in the overridden save method.

$order->save(); // Save the record to the database
shariff's avatar

@jcodes You can try to get the next increment id from a table.

example Using MySql

SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "yourDatabaseName"
AND TABLE_NAME = "yourTableName"

using laravel

use DB;

$statement = DB::select("SHOW TABLE STATUS LIKE 'users'");
$nextId = $statement[0]->Auto_increment;

Snapey's avatar

you cannot do this before the record is saved since it is the database that issues the id

If you must use id then you will need to use two writes and move your observer to the created event

Please or to participate in this conversation.