我收到致命错误"在null"上调用成员函数update()当试图从Laravel中的多步形式发布时。
Routes.php文件的内容
Route::get('accounts/create/{step}', 'AccountsController@getAccountStep')->where(['step' => '[1-3]']);
Route::post('accounts/create/{step}', AccountsController@postAccountStep')->where(['step' => '[1-3]']);
Route::post('accounts', 'AccountsController@postAccount');
Route::get('accounts', 'AccountsController@getAccount');
Route::resource('/accounts', 'AccountsController');
Route::get('accounts/success', 'AccountsController@accountSuccess');
AccountsController.php文件的内容
public function getAccount()
{
return view('accounts.create-1');
}
public function postAccount(Request $request)
{
$this->validate($request, [
'account_no' => 'required|account_no'
]);
$account = Account::firstOrCreate(['account_no' => $request->input('account_no')]);
$request->session()->put('account', $account);
return redirect()->action('AccountsController@getAccountStep', ['step' => 1]);
}
public function getAccountStep(Request $request, $step)
{
return view('accounts.create-'.$step, ['account' => $request->session()->get('account')]);
}
protected $lastStep = 3;
public function postAccountStep(Request $request, $step)
{
switch ($step)
{
case 1:
$rules = ['account_no' => 'required|min:2|max:50'];
break;
case 2:
$rules = ['bill_to' => 'required|min:3'];
break;
case 3:
$rules = ['phone' => 'required|in:Cats,Dogs'];
break;
default:
abort(400, "No rules for this step!");
}
$this->validate($request, $rules);
$request->session()->get('account')
->update($request->all())
;
if ($step == $this->lastStep) {
return redirect()->action('AccountsController@accountSuccess');
}
return redirect()->action('AccountsController@getAccountStep', ['step' => $step+1]);
}
public function accountSuccess(Request $request)
{
echo '<h1>Account Added</h1>';
}
创建-1.blade.php
{!! Form::open() !!}
<div class="row" id="progress-bar">
<img src="/images/dsd-admin-icons/progress-bars/new-acct-1.png" class="img-responsive">
</div>
<div class="form-group">
{!! Form::label('account_no','Account Number') !!}
{!! Form::text('account_no', null, ['class'=>'form-control']) !!}
{{csrf_field()}}
</div>
<div class="form-group">
{!! Form::label('name','Customer Name') !!}
{!! Form::text('name', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('acct_type','Account Type') !!}
{!! Form::select('acct_type', ['cfs' => 'CFS', 'trucking' => 'Trucking'], null, ['class'=>'form-control']) !!}
</div>
{!! Form::submit('Continue', ['class'=>'btn btn-warning']) !!}
{!! Form::close() !!}
答案 0 :(得分:0)
似乎
$request->session()->get('account')
返回null
。因此
$request->session()->get('account')->update()
无法正常工作。
我认为您使用$request->session()->get('account')
存储当前有效的用户帐户,对吧?如果是,请仔细检查会话是否配置正确。由于postAccount()
方法对我来说没问题,我认为会话无法存储数据。