gpt4 book ai didi

php - 当出现一些依赖错误时,如何停止在 Laravel 中执行 Create 操作

转载 作者:行者123 更新时间:2023-12-03 23:08:34 27 4
gpt4 key购买 nike

我正在根据从用户详细信息表中获取的详细信息创建用户,

我的 Controller

 public function store(Request $request)
{

// Section 1

$user = User::create(
[
'name' => $request->input('first_name'),
'email' => $request->input('email'),
'row_status' => '1',
'user_type' => '3',
'password' => bcrypt('RandomPassword@123')

]
);

// Section 2

$technician_register = new Technician();
$technician_register->user_id = $user->id;
$technician_register->first_name = $request->input('first_name');
if ($request->hasFile('avatar_image')) {
$avatar_image = $request->file('avatar_image');
$extension = $avatar_image->getClientOriginalExtension();
$filename = $request->input('first_name') . date('Y-M-d') . '.' . Str::random(3) . "." . $extension;
$location = public_path('assets/images/technician_avatar/' . $filename);
Imag::make($avatar_image)->resize(300, 300)->save($location); // the Image is spelled wrong purposefully to replicate an error.
$technician_register->avatar_image = $filename;
}
$technician_register->save();

if ($technician_register) {
Session::flash('success', 'Selected record has been successfully saved');
return back();
} else {
$user_delete = User::findOrFail($user->id);
$user_delete->delete();
Session::flash('error', 'Error');
return back();

//This block doesn't seem to work!!
}

}

我在执行第 1 部分的第二部分中遇到错误。如果出现任何错误,如何停止或逆转操作的发生

最佳答案

这就是交易非常方便的地方。您可以使用 \DB::beginTransaction(),然后使用 \DB::rollback() 来阻止保存(如果出现问题),或者 \DB::commit() 完成保存(如果一切顺利)。

基本示例是:

public function store(Request $request){
\DB::beginTransaction();
try {
$user = User::create(
[
'name' => $request->input('first_name'),
'email' => $request->input('email'),
'row_status' => '1',
'user_type' => '3',
'password' => bcrypt('RandomPassword@123')
]
);

$technician_register = new Technician();

$technician_register->user_id = $user->id;
$technician_register->first_name = $request->input('first_name');
if ($request->hasFile('avatar_image')) {
$avatar_image = $request->file('avatar_image');
$extension = $avatar_image->getClientOriginalExtension();
$filename = $request->input('first_name') . date('Y-M-d') . '.' . Str::random(3) . "." . $extension;
$location = public_path('assets/images/technician_avatar/' . $filename);
Image::make($avatar_image)->resize(300, 300)->save($location);
$technician_register->avatar_image = $filename;
}

$technician_register->save();
} catch (\Exception $ex){
\Log::error("Error in store(): ".$ex->getMessage());

\DB::rollBack();

Session::flash('error', 'Error');
return back();
}

\DB::commit();

Session::flash('success', 'Selected record has been successfully saved');
return back();
}

当您使用 DB::beginTransaction 时,结合 try { ... } catch { ... } block ,您可以尝试保存,并且发生错误时(现在已正确捕获,以前没有),您可以阻止保存数据并防止“困惑”数据(没有相关记录的记录等)

编辑:您有这行代码:

  Imag::make($avatar_image)->resize(300, 300)->save($location);
// the Image is spelled wrong purposefully to replicate an error.

但是如果没有 try/catch,就会产生 Uncaught Error 并停止执行,这意味着永远不会到达delete 逻辑。

关于php - 当出现一些依赖错误时,如何停止在 Laravel 中执行 Create 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59055498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com