Reputation: 521
How can I make sure the PHP code continue to run even if the $model->save() fails?
I have a unique index on the url column, so my code will fail on purpose sometimes, but I want to make sure the code doesn't stop because of that.
$url = new \App\Url;
$url->url = 'http://www.example.com';
$url->save();
I tried try catch like this:
$url = new \App\Url;
$url->url = 'http://www.example.com';
try {
$url->save();
} catch (Exception $e) {
//
}
But it still throws a MySQL error and the code stops.
QueryException in Connection.php line 614:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'http://www.example.com' for key 'url_index' (SQL: insert into `urls` (`url`, `updated_at`, `created_at`) values (http://www.example.com, 2015-02-15 09:25:40, 2015-02-15 09:25:40))
Upvotes: 0
Views: 460
Reputation: 111899
I think might be a problem with namespace here, try something like this:
$url = new \App\Url;
$url->url = 'http://www.example.com';
try {
$url->save();
} catch (\Exception $e) {
//
}
Upvotes: 1