简体   繁体   English

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.我想通过下一个和上一个按钮浏览它们。

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 并增加它的最佳方法是什么?

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>
// 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();

I understand the approach being taken here by user2581096 but I am not sure it is efficient (by any standards).我理解 user2581096 在这里采用的方法,但我不确定它是否有效(以任何标准衡量)。 We are calling the database 3 times for really no good reason.我们毫无理由地调用了数据库 3 次。 I suggest an alternative that will be way more efficient and scalable.我建议采用一种更高效和可扩展的替代方案。

Do not pass the previous and next IDs to the view.不要将上一个和下一个 ID 传递给视图。 This eliminates 2 unnecessary database calls.这消除了 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.因此,数据库调用仅在您需要实际查找用户时进行。

// 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();

To get next and previous post we can use max and min functions on Model id in laravel.为了获得下一篇和上一篇文章,我们可以在 laravel 中的Model id上使用maxmin函数。 here is an example to get this https://usingphp.com/post/get-next-and-previous-post-link-in-laravel The Controller:这是获取此https://usingphp.com/post/get-next-and-previous-post-link-in-laravel控制器的示例:

public function post($id)
{
    $post = Post::find($id);
    $previous = Post::where('id', '<', $post->id)->max('id');
    $next = Post::where('id', '>', $post->id)->min('id');
    return view( 'post', compact( 'post', 'next', 'previous' ));
}

The View:风景:

@if($next)
   <a href="{{ route( 'blog.show', $next->id ) }}">{{$next->title}}</a>
@endif
@if($previous)
   <a href="{{ route( 'blog.show', $previous->id ) }}">{{$previous->title}}</a>
@endif

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');看起来您要使用$next = User::where('id', '>', $id)->min('id');$next = User::where('id', '>', $id)->min('id'); and have the view as: <a href="{{ URL::to( 'users/' . $next->id ) }}">Next</a>并有这样的观点: <a href="{{ URL::to( 'users/' . $next->id ) }}">Next</a>

Also don't forget to pass $next to the view.也不要忘记在视图$next传递$next

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);
});

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 builder is 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。

First, get a record out of the database.首先,从数据库中获取一条记录。

    $post = Post::where('slug', $slug)->first();

With a database record, we can get the previous record where the record id is less than the id stored inside $post order by the id in descending order and use first() to get a single record back.使用数据库记录,我们可以通过 id 按降序获取记录 id 小于 $post order 中存储的 id 的前一条记录,并使用 first() 获取单个记录。

    $previous = Post::where('id', '<', $post->id)->orderBy('id','desc')->first();

To get the next record it's almost the same query, this time get the record where the id is more than the id stored in $post.要获取下一条记录,它几乎是相同的查询,这次获取 id 大于 $post 中存储的 id 的记录。

    $next = Post::where('id', '>', $post->id)->orderBy('id')->first();

Controller:控制器:

public function show($id)
{
    // get the current user
    $user = User::find($id);

    // get previous user id
    $previous = User::offset($user->id-2)->first();

    // get next user id
    $next = User::offset($user->id)->first();

    return View::make('users.show')->with('previous', $previous)->with('next', $next);
}

In your App\\Models\\User.php在您的App\\Models\\User.php

...
protected $appends = ['next', 'previous'];

public function getNextAttribute()
{
    return $this->where('id', '>', $this->id)->orderBy('id','asc')->first();
}

public function getPreviousAttribute()
{
    return $this->where('id', '<', $this->id)->orderBy('id','asc')->first();
}

In your Controller you can simply do this:在您的控制器中,您可以简单地执行以下操作:

public function show(User $user)
{
    return View::make('users.show')
    ->with('user', $user)
    ->with('previous', $previous)
    ->with('next', $next);
}

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM