Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 路由正则表达式检查数据库中是否存在id_Php_Laravel_Laravel 4 - Fatal编程技术网

Php 路由正则表达式检查数据库中是否存在id

Php 路由正则表达式检查数据库中是否存在id,php,laravel,laravel-4,Php,Laravel,Laravel 4,如果只希望接受管线的特定参数,请执行以下操作: Route::get('users/{id}', 'UsersController@show')->where('id', '\d+'); 但是如果我想要像这样的东西呢: Route::get('users/{id}', 'UsersController@show')->where('id', '\d+')->where(this id exists in table column id); laravel是如何做到这一点的?

如果只希望接受管线的特定参数,请执行以下操作:

Route::get('users/{id}', 'UsersController@show')->where('id', '\d+');
但是如果我想要像这样的东西呢:

Route::get('users/{id}', 'UsersController@show')->where('id', '\d+')->where(this id exists in table column id);
laravel是如何做到这一点的?
如果数据库中还没有id,我想返回未找到的页面


更新

我想创建一个过滤器:

Route::filter('user.exists', function()
{
//get the id then check the database
});
但是如何将$id传递给过滤器


我知道我可以做一些事情,比如检查查询是否返回了控制器中的任何数据,如果现在这样,则抛出404:

$user = User::find($id);

if(!$user){
    App::abort(404);
}

但我认为如果我能在路线上做到这一点,它会更干净。因为我不仅要过滤用户,还要过滤更多内容。

这将帮助您开始:

Route::get('users/{id}', 'UsersController@show', array('before' => 'userExists'))->where('id', '[\d]+');

Route::filter('userExists', function($id) {
  echo $id; exit;
});