Php Laravel 5.2执行多个db事务,如果失败,则提交或回滚
我正在学习拉威尔,学习5.22。我试图将两条记录保存到两个表中,但只有在两个方面都成功的情况下才提交更改,否则我希望它失败并回滚 我的保存控制器代码为: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::
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