错误:传递给App \ Http \ Controllers \ mycontroller :: cH()的参数3必须是Illuminate \ Http \ Request的实例,没有给出

时间:2016-12-02 11:44:39

标签: php laravel

我收到上面提到的错误,这里是我的方法代码

    function customHeader($id, $user_id, Request $request){

        if ($user_id == Auth::user()->id || Auth::user()->is_admin){

        $method = $request->method();
        // To show View
        if($request->isMethod('GET')){


     }      // Create
     elseif($request->isMethod('POST')){

            // Delete
     } elseif($request->isMethod('Delete')){
                //Delete single

    } else{
            return redirect()->back();
    }
}

上面的函数参数id仅在用户需要删除记录时才需要..我认为错误是由于id而我需要id变量。如果我删除那个$ id它适用于创建如何修复该错误

路线为

Route::any('/setting/custom-header/{id?}', 'SettingController@customHeader');

1 个答案:

答案 0 :(得分:2)

function customHeader(Request $request, $id, $user_id = null){

        if ($user_id == Auth::user()->id || Auth::user()->is_admin){

        $method = $request->method();
        // To show View
        if($request->isMethod('GET')){


     }      // Create
     elseif($request->isMethod('POST')){

            // Delete
     } elseif($request->isMethod('Delete')){
                //Delete single

    } else{
            return redirect()->back();
    }
}

更改参数顺序make request as first param,并赋予参数默认值。