【问题标题】:Argument 2 passed to Controller method must be in instance of Request, none given传递给 Controller 方法的参数 2 必须在 Request 实例中,没有给出
【发布时间】:2017-01-02 18:40:38
【问题描述】:

我试图在我的控制器中将 2 个日期从一个视图传递到另一个视图,但出现以下错误: 传递给 App\Http\Controllers\GuestController::reservationToGuest() 的参数 2 必须是 Illuminate\Http\Request 的实例,没有给出

这是我的第一个视图(有日期的视图):

<form action="{{ route('create_guest') }}">                                            
    {{ csrf_field() }}
    <input type="hidden" value="2016-08-26" name="dateOne">
    <input type="hidden" value="2016-08-28" name="dateTwo">
    <button class="btn btn-block btn-success">Proceed To Check In</button>
</form>

(dateOne 和 dateTwo 是第二个视图中我想要的日期)

routes.php

Route::get('/guest_page/create/{idreservation?}',[
    'uses' => 'GuestController@reservationToGuest',
    'as' => 'create_guest'
]);

GuestController 中的reservationToGuest

public function reservationToGuest($idreservation = null, Request $request){
    if($idreservation === null){
        return view('guest_page_create', ['idreservation' => 0, 'page_title' => 'Guest Check In', 'check_in_date' => $request['dateOne']]);

    } else{ //else clause works just fine and the dates from the database are inserted into the view
        $details = DB::table('reservation')
            ->where('idreservation', $idreservation)
            ->get();

        return view('guest_page_create', ['idreservation' => $idreservation, 'details' => $details, 'page_title' => 'Guest Check In']);
    }
}

在视图中'guest_page_create'

<label for="datemask">Check In Date</label>
<input type="date" class="form-control" id="datemask" name="datemask" value="{{ $check_in_date }}">

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您不应在必需参数之前传递可选参数。试试这个:

    public function reservationToGuest(Request $request, $idreservation = null)
    {
        // ...
    }
    

    【讨论】:

    • 非常感谢。那成功了。一旦网站允许我接受你的回答,我就会接受。我不知道您必须在非可选参数之后传递可选参数。再次感谢。
    • 这确实奏效了,但我仍然很惊讶在我的一些 Laravel 项目中它没有任何特定的顺序就可以工作。
    猜你喜欢
    • 2017-09-26
    • 2018-08-14
    • 2017-04-06
    • 2017-12-24
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2015-10-16
    • 2018-06-01
    相关资源
    最近更新 更多