【发布时间】:2023-03-27 02:03:01
【问题描述】:
这是我正在使用的代码:
for ($i=0; $i < 2; $i++) {
try {
User::create([
'name' => 'username'
]);
break;
} catch (\Illuminate\Database\QueryException $e) {
if ($i == 1) {
throw $e;
}
}
}
但我想使此代码可重用,例如仅替换 try 循环中的数据:
for ($i=0; $i < 2; $i++) {
try {
// replace something here
// all rest of the code should be ignore and resuable
break;
} catch (\Illuminate\Database\QueryException $e) {
if ($i == 1) {
throw $e;
}
}
}
可能是这样的用例:
loop(function() {
User::create([
'name' => 'user name'
]);
})
有什么办法吗?
【问题讨论】: