Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Php Laravel 5.2执行多个db事务,如果失败,则提交或回滚_Php_Laravel 5.2 - Fatal编程技术网

Php Laravel 5.2执行多个db事务,如果失败,则提交或回滚

Php Laravel 5.2执行多个db事务,如果失败,则提交或回滚,php,laravel-5.2,Php,Laravel 5.2,我正在学习拉威尔,学习5.22。我试图将两条记录保存到两个表中,但只有在两个方面都成功的情况下才提交更改,否则我希望它失败并回滚 我的保存控制器代码为: public function store(Request $request) { $all = $request->all(); // we need to fill in who is the creator of this new user, $all['creator_user_id'] = Auth::

我正在学习拉威尔,学习5.22。我试图将两条记录保存到两个表中,但只有在两个方面都成功的情况下才提交更改,否则我希望它失败并回滚

我的保存控制器代码为:

public function store(Request $request)
{
    $all = $request->all();

    // we need to fill in who is the creator of this new user,
    $all['creator_user_id'] = Auth::user()->id;

    // Commit both updates or fail and rollback
    DB::transaction(function ($all) {
        $client = Client::create($all);
        $orgClient['organisation_id'] = $client->organisation_id;
        $orgClient['client_id'] = $client->client_id;
        OrganisationClient::create($orgClient);
    });

    return redirect()
        ->route('client.index')
        ->withMessage([
            'type' => 'success',
            'value' => 'Client <strong>' . $all->client_name . '</strong> successfully created.']);

}

我的问题似乎是将
$all
传递给闭包。如果从closure参数中删除
$all
,则会得到
未定义的变量all
。我该怎么做?谢谢

您正在将
$all
设置为回调参数,而不是
使用它。此时,
事务
回调正在接收
illighte\Database\Connection
的实例作为参数

为了获得所需的实际变量,必须将回调更改为:

/。。。。
DB::事务(函数()使用($all){
// ...

您正在将
$all
设置为回调参数,而不是
使用它。
事务
回调此时正在接收一个
照亮\数据库\连接
的实例作为参数

为了获得所需的实际变量,必须将回调更改为:

/。。。。
DB::事务(函数()使用($all){
// ...
Type error: Argument 1 passed to Illuminate\Database\Eloquent\Model::create() must be of the type array, object given, called in /home/vagrant/Code/simply-invoice/app/Http/Controllers/ClientController.php on line 80