在laravel控制器中缺少参数1

时间:2015-04-02 05:03:12

标签: laravel

在routes.php中,我有以下路线

Route:: get('/crm/hotel/occupant/{id}', array('uses'=>'OccupantController@occupant','as'=>'crm.hotel.occupant'));

对于上述路线...如果我把这样的控制器放在它的工作......但是如果我删除模型中的$ room_id就像

  

$ hotel = new Occupant();

..我在错误中错过了参数1 ....

public function occupant($room_id)
    {
        $hotel = new Occupant($room_id);
        // manage page 
        return $hotel->occupant($room_id);
    }

如何解决......

2 个答案:

答案 0 :(得分:0)

您可以{id}可选。

这是通过

实现的
 Route:: get('/crm/hotel/occupant/{id?}',     array('uses'=>'OccupantController@occupant', 'as'=>'crm.hotel.occupant'));

答案 1 :(得分:0)

正如@vinweb所解释的那样,您必须在?参数中添加问号idRoute:: get('/crm/hotel/occupant/{id?}', array('uses'=>'OccupantController@occupant','as'=>'crm.hotel.occupant'));

但您还必须将变量设置为默认值(示例取自official doc):

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});

所以,在你的情况下,它可能是这样的:

public function occupant($room_id = null)
{
    $hotel = new Occupant($room_id);
    // manage page 
    return $hotel->occupant($room_id);
}
相关问题