How to bypass database violations?
I have a table with unique index, I am trying to update the model in a try-catch block
$exceptions = 0;
try {
AssetDepreciation::updateOrCreate([
'asset_id' => $asset->id,
'as_on' => $data['as_on_date'],
'depreciated_value' => (($asset->asset_depreciations()->count() > 0 ? $asset->asset_depreciations()->orderBy('as_on','desc')->first()->depreciated_value : $asset->purchase_value) * (100 - $asset->depreciation_percentage) / 100),
]);
} catch (Exception $e) {
$exceptions++;
}
but it does not bypass "SQLSTATE[23000]: Integrity constraint violation:"
why?
3 Replies
Please read our #✅┊rules on code formatting
Why would you even do this? You should validate your data before it hits the database
OK, I have updated the code and check for the existing unique before updating. But I wonder, why can't the try-catch block do that for me? what is the use of try-catch then?
My guess: You didn't import
Exception