Laravel-如何传递参数以返回重定向

时间:2020-02-20 14:10:26

标签: laravel

在我的Laravel-5.8中,我有:

public function manager_employee_goal_list($id)
{
    $goal = AppraisalGoal::findOrFail($id);
    $goaldetails = AppraisalGoalDetail::where('appraisal_goal_id', $id)->get();

    return view('appraisal.appraisal_goals.manager_employee_goal_list')
        ->with('goal', $goal)
        ->with('goaldetails', $goaldetails);       
} 

public function manager_employee_goal_approve(Request $request, $id)
{
    $goal = AppraisalGoal::find($id);
    $goal->is_approved = 3;
    $goal->line_manager_comment = $request->line_manager_comment;
    $goal->save();

    Session::flash('success', 'Goal is approved');

    return redirect()->route('appraisal.appraisal_goals.manager_employee_goal_list');        
} 

视图:manager_employee_goal_list

<a class="btn btn-xs btn-info" data-toggle="modal" data-target="#approve{{ $goal->id }}" data-original-title="Approve">
   Approve
</a>

<div class="modal fade" id="approve{{ $goal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{route('appraisal.appraisal_goals.manager_employee_goal_approve',['id'=>$goal->id])}}" method="post">
                {{ csrf_field() }}
                <div class="modal-header">
                    Approve Goal
                </div>
                <div class="col-md-12">
                    <div class="form-group">
                        <label class="control-label">Comment</label>
                        <textarea rows="2" name="line_manager_comment" class="form-control" placeholder="Enter Comment here" value="{{old('line_manager_comment')}}" required data-validation-required-message="This field is required"></textarea> 
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button  type="submit" class="btn btn-success btn-ok">Approve Goal</button>
                </div>
            </form>
        </div>
    </div>
</div>

route / web.php

Route::get('appraisal_goals/manager_employee_goal_list/{id?}', 'Appraisal\AppraisalGoalsController@manager_employee_goal_list')->name('appraisal.appraisal_goals.manager_employee_goal_list');
Route::post('appraisal_goals/manager_employee_goal_approve/{id?}', 'Appraisal\AppraisalGoalsController@manager_employee_goal_approve')->name('appraisal.appraisal_goals.manager_employee_goal_approve');

当我在模态表单上提交时,它会保存但无法重定向到:

return redirect()-> route('appraisal.appraisal_goals.manager_employee_goal_list');

它显示此错误

函数App \ Http \ Controllers \ Appraisal \ AppraisalGoalsController :: manager_employee_goal_list()的参数太少,已传递0个且恰好期望1个

我该如何解决?

谢谢

1 个答案:

答案 0 :(得分:0)

manager_employee_goal_list方法需要一个ID,您要做的就是传递该ID,如下所示:

return redirect()->route(
        'appraisal.appraisal_goals.manager_employee_goal_list',
        ['id' => $id]
);

我还建议您在定义的路由中删除id之后的问号,或通过将其设置为可空值来更新方法中的参数

public function manager_employee_goal_list($id = null)

当然,您也必须调整代码