函数的参数太少,在 Laravel 中传递了 0 个,预期正好是 1 个
Too few arguments to function, 0 passed and exactly 1 expected in Laravel
我在Laravel-5.8有一个项目。
'actionURL' => route('appraisal.appraisal_goals.goal_adjustment_self_review', ['id'=>$employeeId]),
从 Notification actionUrl,我将 ['id'=>$employeeId]
传递到:
public function goal_adjustment_self_review($id)
{
$goals = AppraisalGoal::where('employee_id', $id)->whereNull('deleted_at')->get();
return view('appraisal.appraisal_goals.goal_adjustment_self_review')->with(['goals' => $goals]);
}
然后我从 goal_adjustment_self_review($id)
生成了这个编辑控制器函数
public function goal_adjustment_edit($id)
{
$userCompany = Auth::user()->company_id;
$userId = Auth::user()->employee_id;
$goal = AppraisalGoal::findOrFail($id);
return view('appraisal.appraisal_goals.goal_adjustment_edit')
->with('goal', $goal);
}
public function goal_adjustment_update(UpdateAppraisalGoalAdjustmentRequest $request, $id)
{
DB::beginTransaction();
try {
$goal = AppraisalGoal::findOrFail($id);
$goal->goal_type_id = $request->goal_type_id;
$goal->weighted_score = $request->weighted_score;
$goal->save();
DB::commit();
Session::flash('success', 'Goal Setting Weight is updated successfully');
return redirect()->route('appraisal.appraisal_goals.goal_adjustment_self_review');
} catch (Exception $exception) {
Log::error($exception);
DB::rollback();
Session::flash('error', 'Action failed! Please try again');
return redirect()->route('appraisal.appraisal_goals.goal_adjustment_self_review');
}
}
查看:goal_adjustment_edit
<form action="{{route('appraisal.appraisal_goals.goal_adjustment_update', ['id'=>$goal->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Weight(%):<span style="color:red;">*</span></label> <input type="hidden" id="goal_weight_balance" value="0" disabled>
<input id="total_weighted_score" type="text" name="weighted_score" value="{{old('weighted_score',$goal->weighted_score)}}" placeholder="Enter weighted score here" class="form-control" max="120" onkeyup="checkScore(this.value)">
</div>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
路线:
Route::group(['prefix' => 'appraisal', 'as' => 'appraisal.', 'namespace' => 'Appraisal', 'middleware' => ['auth']], function () {
Route::get('appraisal_goals/goal_adjustment_self_review/{id?}', 'AppraisalGoalsController@goal_adjustment_self_review')->name('appraisal_goals.goal_adjustment_self_review');
Route::get('appraisal_goals/goal_adjustment_edit/{id?}', 'AppraisalGoalsController@goal_adjustment_edit')->name('appraisal_goals.goal_adjustment_edit');
Route::put('appraisal_goals/goal_adjustment_update/{id?}', 'AppraisalGoalsController@goal_adjustment_update')->name('appraisal_goals.goal_adjustment_update');
});
当我提交上面的更新表单时,我得到了这个错误:
[2020-12-18 20:10:46] production.ERROR: Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::goal_adjustment_self_review(),
0 passed and exactly 1 expected
对于这一行:
public function goal_adjustment_self_review($id)
如何解决这个问题?
错误很明显,您没有将参数传递给该方法。该方法由路由引擎调用,这意味着您没有传递您应该传递的路由参数。
您的控制器方法 goal_appraisal_update()
包含此行(出于某种原因两次):
return redirect()->route('appraisal.appraisal_goals.goal_adjustment_self_review');
参数在哪里?
你应该做的是使用 route model binding 所以你的控制器方法看起来像这样:
public function goal_adjustment_self_review($employee)
{
$goals = $employee->appraisal_goals;
return view('appraisal.appraisal_goals.goal_adjustment_self_review')
->with('goals', $goals);
}
public function goal_adjustment_edit(AppraisalGoal $goal)
{
return view('appraisal.appraisal_goals.goal_adjustment_edit')
->with('goal', $goal);
}
public function goal_adjustment_update(UpdateAppraisalGoalAdjustmentRequest $request, AppraisalGoal $goal)
{
try {
$goal->update($request->only('goal_type_id', 'weighted_score'));
Session::flash('success', 'Goal Setting Weight is updated successfully');
} catch (\Exception $exception) {
Log::error($exception);
Session::flash('error', 'Action failed! Please try again');
}
return redirect()
->route('appraisal.appraisal_goals.goal_adjustment_self_review', $goal);
}
然后路由可以这样定义:
Route::group(['prefix' => 'appraisal', 'as' => 'appraisal.', 'namespace' => 'Appraisal', 'middleware' => ['auth']], function () {
Route::get('appraisal_goals/goal_adjustment_self_review/{employee}', 'AppraisalGoalsController@goal_adjustment_self_review')
->name('appraisal_goals.goal_adjustment_self_review');
Route::get('appraisal_goals/goal_adjustment_edit/{goal}', 'AppraisalGoalsController@goal_adjustment_edit')
->name('appraisal_goals.goal_adjustment_edit');
Route::put('appraisal_goals/goal_adjustment_update/{goal}', 'AppraisalGoalsController@goal_adjustment_update')
->name('appraisal_goals.goal_adjustment_update');
});
注意路由参数与方法参数的名称匹配。当在方法签名中声明类型时,奇迹就会发生。
我在Laravel-5.8有一个项目。
'actionURL' => route('appraisal.appraisal_goals.goal_adjustment_self_review', ['id'=>$employeeId]),
从 Notification actionUrl,我将 ['id'=>$employeeId]
传递到:
public function goal_adjustment_self_review($id)
{
$goals = AppraisalGoal::where('employee_id', $id)->whereNull('deleted_at')->get();
return view('appraisal.appraisal_goals.goal_adjustment_self_review')->with(['goals' => $goals]);
}
然后我从 goal_adjustment_self_review($id)
生成了这个编辑控制器函数public function goal_adjustment_edit($id)
{
$userCompany = Auth::user()->company_id;
$userId = Auth::user()->employee_id;
$goal = AppraisalGoal::findOrFail($id);
return view('appraisal.appraisal_goals.goal_adjustment_edit')
->with('goal', $goal);
}
public function goal_adjustment_update(UpdateAppraisalGoalAdjustmentRequest $request, $id)
{
DB::beginTransaction();
try {
$goal = AppraisalGoal::findOrFail($id);
$goal->goal_type_id = $request->goal_type_id;
$goal->weighted_score = $request->weighted_score;
$goal->save();
DB::commit();
Session::flash('success', 'Goal Setting Weight is updated successfully');
return redirect()->route('appraisal.appraisal_goals.goal_adjustment_self_review');
} catch (Exception $exception) {
Log::error($exception);
DB::rollback();
Session::flash('error', 'Action failed! Please try again');
return redirect()->route('appraisal.appraisal_goals.goal_adjustment_self_review');
}
}
查看:goal_adjustment_edit
<form action="{{route('appraisal.appraisal_goals.goal_adjustment_update', ['id'=>$goal->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Weight(%):<span style="color:red;">*</span></label> <input type="hidden" id="goal_weight_balance" value="0" disabled>
<input id="total_weighted_score" type="text" name="weighted_score" value="{{old('weighted_score',$goal->weighted_score)}}" placeholder="Enter weighted score here" class="form-control" max="120" onkeyup="checkScore(this.value)">
</div>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
路线:
Route::group(['prefix' => 'appraisal', 'as' => 'appraisal.', 'namespace' => 'Appraisal', 'middleware' => ['auth']], function () {
Route::get('appraisal_goals/goal_adjustment_self_review/{id?}', 'AppraisalGoalsController@goal_adjustment_self_review')->name('appraisal_goals.goal_adjustment_self_review');
Route::get('appraisal_goals/goal_adjustment_edit/{id?}', 'AppraisalGoalsController@goal_adjustment_edit')->name('appraisal_goals.goal_adjustment_edit');
Route::put('appraisal_goals/goal_adjustment_update/{id?}', 'AppraisalGoalsController@goal_adjustment_update')->name('appraisal_goals.goal_adjustment_update');
});
当我提交上面的更新表单时,我得到了这个错误:
[2020-12-18 20:10:46] production.ERROR: Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::goal_adjustment_self_review(),
0 passed and exactly 1 expected
对于这一行:
public function goal_adjustment_self_review($id)
如何解决这个问题?
错误很明显,您没有将参数传递给该方法。该方法由路由引擎调用,这意味着您没有传递您应该传递的路由参数。
您的控制器方法 goal_appraisal_update()
包含此行(出于某种原因两次):
return redirect()->route('appraisal.appraisal_goals.goal_adjustment_self_review');
参数在哪里?
你应该做的是使用 route model binding 所以你的控制器方法看起来像这样:
public function goal_adjustment_self_review($employee)
{
$goals = $employee->appraisal_goals;
return view('appraisal.appraisal_goals.goal_adjustment_self_review')
->with('goals', $goals);
}
public function goal_adjustment_edit(AppraisalGoal $goal)
{
return view('appraisal.appraisal_goals.goal_adjustment_edit')
->with('goal', $goal);
}
public function goal_adjustment_update(UpdateAppraisalGoalAdjustmentRequest $request, AppraisalGoal $goal)
{
try {
$goal->update($request->only('goal_type_id', 'weighted_score'));
Session::flash('success', 'Goal Setting Weight is updated successfully');
} catch (\Exception $exception) {
Log::error($exception);
Session::flash('error', 'Action failed! Please try again');
}
return redirect()
->route('appraisal.appraisal_goals.goal_adjustment_self_review', $goal);
}
然后路由可以这样定义:
Route::group(['prefix' => 'appraisal', 'as' => 'appraisal.', 'namespace' => 'Appraisal', 'middleware' => ['auth']], function () {
Route::get('appraisal_goals/goal_adjustment_self_review/{employee}', 'AppraisalGoalsController@goal_adjustment_self_review')
->name('appraisal_goals.goal_adjustment_self_review');
Route::get('appraisal_goals/goal_adjustment_edit/{goal}', 'AppraisalGoalsController@goal_adjustment_edit')
->name('appraisal_goals.goal_adjustment_edit');
Route::put('appraisal_goals/goal_adjustment_update/{goal}', 'AppraisalGoalsController@goal_adjustment_update')
->name('appraisal_goals.goal_adjustment_update');
});
注意路由参数与方法参数的名称匹配。当在方法签名中声明类型时,奇迹就会发生。