Laravel上一个和下一个记录 [英] Laravel previous and next records
问题描述
我正在尝试创建一个页面,在这里我可以看到数据库中的所有人员并对其进行编辑.我制作了一个表单,在其中填写了某些字段的数据库中的数据.
I am trying to create a page where I can see all the people in my database and create edits on them. I made a form where I fill in the data from the database of certain fields.
我想通过下一个"和上一个"按钮浏览它们.
I would like to navigate trough them by a Next and Previous button.
要生成下一步,我必须使用大于当前ID的ID来加载下一个配置文件.
For generating the next step I have to take the ID larger than the current one to load the next profile.
要生成上一步,我必须使用小于当前ID的ID来加载上一个配置文件.
For generating the previous step I have to take the ID smaller than the current one to load the previous profile.
我的路线:
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并将其递增的最佳方法是什么?
What is the best approach to get the current ID and increment it?
推荐答案
下面是您更新后的控制器,并查看了来自@ ridecar2链接的文件,
Below are your updated controller and view files derived from @ridecar2 link,
控制器:
public function show($id)
{
// get the current user
$user = User::find($id);
// get previous user id
$previous = User::where('id', '<', $user->id)->max('id');
// get next user id
$next = User::where('id', '>', $user->id)->min('id');
return View::make('users.show')->with('previous', $previous)->with('next', $next);
}
查看:
<a href="{{ URL::to( 'users/' . $previous ) }}">Previous</a>
<a href="{{ URL::to( 'users/' . $next ) }}">Next</a>
这篇关于Laravel上一个和下一个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!