问题
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