我无法在 Laravel 中使用事务

jay*_*kar 0 php mysql laravel laravel-5

我正在使用 Laravel 从事一个业余爱好项目(商店管理)。我正在尝试使用 DB::beginTransaction() 和 DB::rollback(),但它不起作用。根据我的代码,我认为数据库中不应该填充任何条目。

我已经用谷歌搜索过可能性,但找不到任何解决方案。而且,我的 MySQL 表是 InnoDB

这是我的商店控制器文件。

    class shopController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function getView()
    {
        if(Auth::user()->shop_status==0)
            return View::make('shop')->with('name',json_encode(Auth::user()->Vendor_Detail()->get(['first_name', 'last_name'])));
        else
            return redirect('/home');
    }

    public function addShop(ShopDataRequest $request){
    //get the logged in vendor's model instance. Auth uses Vendor model
            $vendor = Auth::user();
    //create new Shop Model
            $shop = new Shop;
            $shop->name = $request['shop_name'];
            $shop->address = $request->addressLine1;
            $shop->pincode = $request->pincode;
            $shop->phone = $request->phone;
            $shop->shop_type = $request->shop_type;

        DB::beginTransaction();
        try{            
             //save shop details
            $vendor->Shops()->save($shop);
            //throw custom Exception
            throw new \Exception('User not created for account');
        }

        catch (Exception $e){
        //catch exception and rollback
            DB::rollback();
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

型号:
1) 商店:

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Shop extends Authenticatable
{
    protected $connection = 'vendor';
    protected $fillable = [
        'shop_id','vendor_id','name','address','pincode','phone','shop_type'
    ];

    public function Vendor(){
        return $this->belongsTo('App\Models\Vendor','vendor_id','vendor_id');
    }

    public function Products(){
        return $this->belongsToMany('App\Models\Product')->withPivot('mfg_date', 'exp_date','active','quantity');
    }
}
Run Code Online (Sandbox Code Playgroud)

2) 供应商

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Vendor extends Authenticatable
{
    protected $connection = 'vendor';
    protected $primaryKey = 'vendor_id';
    protected $fillable = [
        'email','phone','password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function Vendor_Detail(){
        return $this->hasOne('App\Models\Vendor_Detail','vendor_id','vendor_id');
    }

    public function Shops(){
        return $this->hasMany('App\Models\Shop','vendor_id','vendor_id');
    }
    public function Documents(){
        return $this->hasMany('App\Models\Vendor_document','vendor_id','vendor_id');
    }
}
Run Code Online (Sandbox Code Playgroud)

显示数据库引擎的 MySQL 表详细信息。

mysql> 显示表状态 WHERE Name= '商店'; +--------+--------+--------+------------+--------+-- --------------+-------------+----------------+--- -----------+------------+----------------+--------- ------------+-------------+------------+------------ -------+----------+----------------+--------------------+ | 名称 | 发动机| 版本 | 行格式| 行| 平均行长度| 数据长度| 最大数据长度| 索引长度| 数据免费| 自动增量 | 创建时间 | 更新时间 | 检查时间 | 整理 | 校验和| 创建选项 | 评论 | +--------+--------+--------+------------+--------+-- --------------+-------------+----------------+--- -----------+------------+----------------+--------- ------------+-------------+------------+------------ -------+----------+----------------+--------------------+ | 商店 | InnoDB | 10 | 10 紧凑| 1 | 16384 |
16384 | 0 | 16384 | 0 | 17 | 17 2016-07-03 04:56:27 | 空| 空| utf8_general_ci | utf8_general_ci |
空| | | +--------+--------+--------+------------+--------+-- --------------+-------------+----------------+--- -----------+------------+----------------+--------- ------------+-------------+------------+------------ -------+----------+----------------+---------+ 集合中的 1 行(0.00 秒)

mysql> 显示表状态 WHERE Name= '供应商'; +---------+--------+---------+------------+-----+ ----------------+-------------------------+-----------------+- ------------+------------------------+----------------+-------- --------------+-------------+------------+-------- ---------+----------+----------------+--------------------+ | 名称 | 发动机| 版本 | 行格式| 行| 平均行长度| 数据长度| 最大数据长度| 索引长度| 数据免费| 自动增量 | 创建时间 | 更新时间 | 检查时间 | 整理 | 校验和| 创建选项 | 评论 | +---------+--------+---------+------------+-----+ ----------------+-------------------------+-----------------+- ------------+------------------------+----------------+-------- --------------+-------------+------------+-------- ---------+----------+----------------+--------------------+ | 供应商| InnoDB | 10 | 10 紧凑| 1 | 16384 |
16384 | 0 | 0 | 0 | 6 | 2016-07-07 00:46:08 | 空| 空| utf8_general_ci | utf8_general_ci |
空| | | +---------+--------+---------+------------+-----+ ----------------+-------------------------+-----------------+- ------------+------------------------+----------------+-------- --------------+-------------+------------+-------- ----------+----------+----------------+--------------------+ 1 行已设置(0.00 秒)

请帮忙。

jay*_*kar 5

我发现问题出在我的连接上。我在模型中使用自定义连接,而不是默认连接。当在 Laravel 中使用数据库外观时,我认为它使用的是默认连接,即mysql而不是供应商

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Shop extends Authenticatable
{
    protected $connection = 'vendor';\\custom connection
    protected $fillable = [
        'shop_id','vendor_id','name','address','pincode','phone','shop_type'
    ];

    public function Vendor(){
        return $this->belongsTo('App\Models\Vendor','vendor_id','vendor_id');
    }

    public function Products(){
        return $this->belongsToMany('App\Models\Product')->withPivot('mfg_date', 'exp_date','active','quantity');
    }
}
Run Code Online (Sandbox Code Playgroud)

我的数据库配置文件中的连接:

'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => 'shop',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => 'InnoDB',
        ],

        'vendor' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => 'shop',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => 'InnoDB',
        ]
    ],
Run Code Online (Sandbox Code Playgroud)

现在,我的商店控制器通过以下方式调用交易。

 class shopController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function getView()
    {
        if(Auth::user()->shop_status==0)
            return View::make('shop')->with('name',json_encode(Auth::user()->Vendor_Detail()->get(['first_name', 'last_name'])));
        else
            return redirect('/home');
    }

    public function addShop(ShopDataRequest $request){
    //get the logged in vendor's model instance. Auth uses Vendor model
            $vendor = Auth::user();
    //create new Shop Model
            $shop = new Shop;
            $shop->name = $request['shop_name'];
            $shop->address = $request->addressLine1;
            $shop->pincode = $request->pincode;
            $shop->phone = $request->phone;
            $shop->shop_type = $request->shop_type;

        //getting the required connection
        $connection = DB::connection('vendor');

        $connection::beginTransaction();//calling the beginTransaction() on connection
        try{            
             //save shop details
            $vendor->Shops()->save($shop);
            //throw custom Exception
            throw new \Exception('User not created for account');
        }

        catch (Exception $e){
        //catch exception and rollback
            $connection::rollback(); //calling the rollback() on connection
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

并且它工作完美。