callbackBeforeUpdate

callbackBeforeUpdate(callable $callback)

The callback is used in cases we need to add, change or filter data before the update functionality. There are basically two types of values that the developer will receive from the callback:

  1. The primaryKeyValue: This is the primary key value that the update will take place.
  2. The data: These are the filtered data from the update

More specifically, below you can see an example of the $stateParameters variable for the callbackBeforeInsert:

$stateParameters = (object)[
    'primaryKeyValue' => '1234', //primary key value
    'data' => [ // data to update
        'customerName' => 'John',
        'phone' => '1234567890',
        ...
    ]
];

A full example is also available for you to understand better. At the below example we are changing the data to append the "[BEFORE UPDATE]" at the city name (just for demonstration really)

$crud->setTable('offices');
$crud->setSubject('Office', 'Offices');
$crud->columns(['city','country','phone','addressLine1','postalCode']);

$crud->callbackBeforeUpdate(function ($stateParameters) {
    $city = $stateParameters->data['city'];

    if (!strstr($city, '[BEFORE UPDATE]')) {
        $stateParameters->data['city'] = '[BEFORE UPDATE] ' . $city;
    }

    return $stateParameters;
});

The result of the above code can be found here: