事务()-Laravel 5中的查询生成器抛出错误 [英] Transaction () - Query Builder in laravel 5 throwing error

查看:193
本文介绍了事务()-Laravel 5中的查询生成器抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 Transaction()创建API,以提高数据库的安全性.

Trying to make an API using Transaction() for making more security for database.

代码:

    DatabaseName::transaction(function () {

   $CheckExistingData = PostCustomer::select('Mobile')->where('Mobile', $Mobile);

  return $CheckExistingData;
  });

错误:

BadMethodCallException in Builder.php line 2258:

Call to undefined method Illuminate\Database\Query\Builder::transaction()

分享您的想法!

PostCustomer是MODEL

PostCustomer is MODEL

PostCustomer模型

class PostCustomer extends Model
{
protected $table = "Customer";
}

"客户"是表名.

推荐答案

我尝试了类似的方法,请尝试使用此代码

I tried something like this, try with this code

PostCustomer::beginTransaction();
try{
    $result = true;
    PostCustomer::commit();
}catch(Exception $exception)
{
    PostCustomer::rollBack();
    $errormsg = 'Database error!! ' . $exception->getMessage();
    $result = false;
}

事务方法无法像这样工作:请参见 https://laravel.com/docs/4.2/database#database-transactions .

Transaction method will not work like this : See this https://laravel.com/docs/4.2/database#database-transactions.

您可以使用transaction methodyou may need to begin a transaction yourself.

这篇关于事务()-Laravel 5中的查询生成器抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆