php Laravel 上一个和下一个记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21909706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel previous and next records
提问by Duikboot
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.
我想通过下一个和上一个按钮浏览它们。
For generating the next step I have to take the ID larger than the current one to load the next profile.
为了生成下一步,我必须使用比当前更大的 ID 来加载下一个配置文件。
For generating the previous step I have to take the ID smaller than the current one to load the previous profile.
为了生成上一步,我必须使用小于当前 ID 的 ID 来加载先前的配置文件。
My route:
我的路线:
Route::get('users/{id}','UserController@show');
Controller:
控制器:
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);
}
View:The buttons:
视图:按钮:
<a href="{{ URL::to( 'users/' . $input->id ) }}">Next</a>
What is the best approach to get the current ID and increment it?
获取当前 ID 并增加它的最佳方法是什么?
回答by
Below are your updated controller and view files derived from @ridecar2 link,
以下是从@ridecar2 链接派生的更新的控制器和视图文件,
Controller:
控制器:
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);
}
View:
看法:
<a href="{{ URL::to( 'users/' . $previous ) }}">Previous</a>
<a href="{{ URL::to( 'users/' . $next ) }}">Next</a>
回答by Alireza Aboutalebi
// in your model file
public function next(){
// get next user
return User::where('id', '>', $this->id)->orderBy('id','asc')->first();
}
public function previous(){
// get previous user
return User::where('id', '<', $this->id)->orderBy('id','desc')->first();
}
// in your controller file
$user = User::find(5);
// a clean object that can be used anywhere
$user->next();
$user->previous();
回答by James Jomuad
// yourModel.php
public function previous()
{
return $this->find(--$this->id);
}
public function next()
{
return $this->find(++$this->id);
}
Works like magic, you can chain it:
像魔术一样工作,你可以链接它:
$prevprev = Model::find($id)->previous()->previous();
$nextnext = Model::find($id)->next()->next();
回答by Nikhil Agarwal
I understand the approach being taken here by user2581096 but I am not sure it is efficient (by any standards). We are calling the database 3 times for really no good reason. I suggest an alternative that will be way more efficient and scalable.
我理解 user2581096 在这里采用的方法,但我不确定它是否有效(以任何标准衡量)。我们毫无理由地调用了数据库 3 次。我建议采用一种更高效和可扩展的替代方案。
Do not pass the previous and next IDs to the view. This eliminates 2 unnecessary database calls.
不要将上一个和下一个 ID 传递给视图。这消除了 2 个不必要的数据库调用。
Create the following routes:
创建以下路由:
users/{id}/next
用户/{id}/下一个
users/{id}/previous
用户/{id}/上一个
These routes should be used in the href attributes of the anchor tags
这些路由应该用在锚标签的 href 属性中
Add methods in the controller to handle each of the new routes you have created. For example:
在控制器中添加方法来处理您创建的每个新路由。例如:
public function getPrevious(){
// get previous user
$user = User::where('id', '<', $this->id)->orderBy('id','desc')->first();
return $this->show($user->id);
}
This function will only be called when you actually click on the button. Therefore, the database call is only made when you need to actually look up the user.
只有当您实际单击按钮时才会调用此函数。因此,数据库调用仅在您需要实际查找用户时进行。
回答by ctf0
in-case you want to retrieve the prev/next records along with their data, you can try
如果您想检索上一条/下一条记录及其数据,您可以尝试
$id = 7; // for example
$prev = DB::table('posts')->where('id', '<', $id)->orderBy('id','desc')->limit(1);
$next = DB::table('posts')->where('id', '>', $id)->limit(1);
$res = DB::table('posts')
->where('id', '=', $id)
->unionAll($prev)
->unionAll($next)
->get();
// now $res is an array of 3 objects
// main, prev, next
dd($res);
1- the query builderis usually much faster than eloquent.
1-查询构建器通常比eloquent快得多。
2- with union we are now only hitting the db once instead of 3.
2- 使用 union 我们现在只访问 db 一次而不是 3。
回答by ridecar2
Here's a link I found that should help: http://maxoffsky.com/code-blog/laravel-quick-tip-get-previous-next-records/
这是我发现应该有帮助的链接:http: //maxoffsky.com/code-blog/laravel-quick-tip-get-previous-next-records/
It looks like for next you want to use: $next = User::where('id', '>', $id)->min('id');and have the view as: <a href="{{ URL::to( 'users/' . $next->id ) }}">Next</a>
看起来您接下来要使用:$next = User::where('id', '>', $id)->min('id');并具有以下视图:<a href="{{ URL::to( 'users/' . $next->id ) }}">Next</a>
Also don't forget to pass $nextto the view.
也不要忘记传递$next给视图。
回答by Alexandre Butynski
Simplest approach
最简单的方法
// User.php
public static function findNext($id)
{
return static::where('id', '>', $id)->first();
}
// UserController.php
$nextUser = User::findNext($id);
// view
<a href="{{ URL::to( 'users/' . $nextUser->id ) }}">Next</a>
Lazy approach :
懒惰的方法:
// view
<a href="{{ URL::to( 'users/' . $input->id . '/next') }}">Next</a>
// routes.php (should be optimized, this is just to show the idea)
Route::get('users/{user}/next', function($id) {
$nextUser = User::findNext($id);
return Redirect::to('user/' . $id);
});
回答by mostafaznv
i developed the code.
我开发了代码。
it work all times, even if we don't have any next or prev post
它始终有效,即使我们没有任何下一篇或上一篇文章
public function nextPost($table, $id)
{
$next = DB::table($table)->where('id', '>', $id)->orderBy('id','asc')->first();
if(!$next)
$next = DB::table($table)->orderBy('id','asc')->first();
return $next;
}
public function prevPost($table, $id)
{
$prev = DB::table($table)->where('id', '<', $id)->orderBy('id','desc')->first();
if(!$prev)
$prev = DB::table($table)->orderBy('id','desc')->first();
return $prev;
}

