php - laravel save() 方法会抛出异常吗?
问题描述
我在这里有一个小问题。这是我的代码:
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
返回一个布尔值,您需要检查保存是否成功,但除非有 mysql 错误,否则您不会收到任何异常。
推荐阅读
- c# - 在 orderby Linq 中检查对象 null
- c# - 如何使用 Sharpmap 将经度和纬度映射到 WGS84?
- php - 如何访问返回的 json 值
- java - 从java执行bash脚本
- java - Spring Boot在测试中没有自动装配类
- vue.js - vue 路由在 chrome 中不起作用...在 IE 和 firefox 中运行良好
- multithreading - Wildfly 10:尝试实现 Java 并发实用程序时出错
- django - 如何根据表达式过滤对象
- javascript - 使用 node 或 express 返回 json 格式的正确方法
- matlab - 标量时间向量积的可绘制性