Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 如果数据库上的id重复,则返回flash消息_Laravel_Controller - Fatal编程技术网

Laravel 如果数据库上的id重复,则返回flash消息

Laravel 如果数据库上的id重复,则返回flash消息,laravel,controller,Laravel,Controller,我想在控制器laravel上创建if-else。如果数据库没有错误,数据插入和返回flash消息成功,如果数据库有错误(重复id),返回错误消息,我使用laravel 5.3 这正是我想要的 这是我的密码 您可以使用DB事务执行try catch try { DB::beginTransaction(); // your code // redirect with success message DB::commit(); }catch (Exception

我想在控制器laravel上创建if-else。如果数据库没有错误,数据插入和返回flash消息成功,如果数据库有错误(重复id),返回错误消息,我使用laravel 5.3

这正是我想要的

这是我的密码


您可以使用
DB事务执行
try catch

try {
    DB::beginTransaction();

    // your code
    // redirect with success message

    DB::commit();
}catch (Exception $e) {

    DB::rollback();

    // other actions
    // redirect with error message

}

大家好,欢迎来到StackOverflow。请花些时间阅读,特别是指定的章节。您可能还想了解如何创建函数和对象数据;创建对象然后保存此数据必须使用此?Illumb\Database\QueryException将代码写入
try
并使用成功消息重定向。将带有错误消息的重定向写入
catch
部分,它的工作是这样的,但我的catch是这样的(catch(\light\Database\QueryException$ex))