Laravel cast route parameter to Integer

若如初见. 提交于 2020-02-04 01:49:14

问题


I really like the idea of Route Model Binding.

Is there a similar way to cast parameters (which are Strings) to Intergers?

The reason is that I often have the parameters year and month, but I would like to have it as an Integer, because the database seems to handle String and Integers in different ways.


回答1:


Yes there is. You can use Route Binding similar to what I answered here. It's unfortunate that it exists but is not documented in the Laravel docs.




回答2:


Update using PHP7, if anyone else comes looking:

public function view(int $id)
{
  echo gettype($id);
  // Outputs "integer"
}


来源:https://stackoverflow.com/questions/33291136/laravel-cast-route-parameter-to-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!