I am trying to do a transaction in the db with Eloquent ORM following the instructions here: https://stackoverflow.com/a/15105781/5649969
I notice that the code throws a Throwable
, so I put it in a try...catch
block to go for an early return if there is an exception.
try {
DB::transaction(function () use ($user, $attributes) {
$validUserAttributes = $user->getFillable();
$updatable = [];
foreach ($attributes as $key => $value) {
if (in_array($key, $validUserAttributes)) {
$updatable[$key] = $value;
}
}
$user->update($updatable);
$validRoleAttributes = $user->role->getFillable();
$updatable = [];
foreach ($attributes as $key => $value) {
if (in_array($key, $validRoleAttributes)) {
$updatable[$key] = $value;
}
}
$user->role()->update($updatable);
});
} catch (Throwable $_) {
dd(1000);
return new UpdateUserResult(false, UpdateUserResult::UPDATE_FAILED);
}
dd(2000);
return new UpdateUserResult(true, UpdateUserResult::UPDATE_SUCCESS);
This is where my problem is, it seems that the early return does not work for whatever reason, when I remove the dd(2000)
, dd(1000)
will run, why does the code seem like it is running from the bottom to the top?
CodePudding user response: