gpt4 book ai didi

php - laravel save() 方法是否抛出异常?

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:06 26 4
gpt4 key购买 nike

我有一个小问题。这是我的代码:

DB::beginTransaction();
try{
$created = new TransportTypeColumn();
$created->name = $translated_ids[0];
if(!$created->save())
throw new \Exception("failed saving transport type column");
DB::commit();
return response()->json(['success'=>'Property has been created successfully', 'data'=>$created],200);

}catch(\Exception $e){
DB::rollback();
return response()->json(['error'=>'Something went wrong, please try later.'], 500);
}

那么我需要这段代码吗? :

if(!$created->save())
throw new \Exception("failed saving transport type column");

或者如果 save() 函数不成功,它会自己抛出异常吗?

最佳答案

save 返回一个 bool 值,你需要检查保存是否成功,但除非有 mysql 错误,否则你不会得到任何异常。

关于php - laravel save() 方法是否抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53724694/

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