【发布时间】:2014-03-21 12:11:48
【问题描述】:
我正在尝试创建一个页面,我可以在其中查看数据库中的所有人并对其进行编辑。我制作了一个表格,在其中填写了某些字段的数据库中的数据。
我想通过“下一个”和“上一个”按钮浏览它们。
为了生成下一步,我必须使用大于当前 ID 的 ID 来加载下一个配置文件。
为了生成上一步,我必须使用小于当前 ID 的 ID 来加载上一个配置文件。
我的路线:
Route::get('users/{id}','UserController@show');
控制器:
public function show($id)
{
$input = User::find($id);
// If a user clicks next this one should be executed.
$input = User::where('id', '>', $id)->firstOrFail();
echo '<pre>';
dd($input);
echo '</pre>';
return View::make('hello')->with('input', $input);
}
查看: 按钮:
<a href="{{ URL::to( 'users/' . $input->id ) }}">Next</a>
获取当前 ID 并增加它的最佳方法是什么?
【问题讨论】:
-
很遗憾,我无法回答这个问题(尽管我可能也会使用您的方法,然后想知道是否有更好的方法),但我认为
firstOrFail最终可能会成为问题:当用户查看最后一个模型,而不是能够看到它时,他们得到一个 404 页面,只是因为没有“下一个”模型可用。
标签: php laravel routing laravel-4