如何在laravel 5中使用分页?

Soj*_*ose 27 php pagination laravel laravel-5

我试图将我的laravel4应用程序移植到laravel 5.在以前的版本中,我可以使用以下方法生成分页URL.

在控制器中:

$this->data['pages']= Page::whereIn('area_id', $suburbs)->where('score','>','0')->orderBy('score','desc')->paginate(12);
Run Code Online (Sandbox Code Playgroud)

在与视图共享数据数组后,我可以用户

在视图中:

{{$pages->links()}}
Run Code Online (Sandbox Code Playgroud)

在laravel 5中,这样做会导致跟随错误

ErrorException in AbstractPaginator.php line 445:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'links'
Run Code Online (Sandbox Code Playgroud)

不知道我在这里缺少什么,有人可以帮忙吗?

小智 62

在Laravel 5中没有方法"链接"你可以试试这个

{!! $pages->render() !!}
Run Code Online (Sandbox Code Playgroud)

  • {!! $ pages-> render()!!}在laravel5中诀窍,另一种语法逃脱了html.谢谢 (12认同)

小智 6

在其他框架中,分页可能非常痛苦.Laravel 5让它变得轻而易举.要使用它,首先必须在从数据库调用数据的控制器代码中进行更改:

 public function index()
{       
        $users = DB::table('books')->simplePaginate(5);
        //using pagination method
        return view('index', ['users' => $users]);
}
Run Code Online (Sandbox Code Playgroud)

...之后你可以使用这段代码:

<?php echo $users->render(); ?>
Run Code Online (Sandbox Code Playgroud)

这将使您使用简单的Laravel 5美.


Nad*_*smi 6

分页 Laravel 5.6.26,对于分页,控制器是:

控制器代码(https://laravel.com/docs/5.6/pagination#basic-usage

    posts = Post::orderBy('created_at','desc')->paginate(10);
    return view('posts.index')->with('posts', $posts);
Run Code Online (Sandbox Code Playgroud)

前端进入刀片(视图)(https://laravel.com/docs/5.6/pagination#displaying-pagination-results

   {{ $users->links() }}
Run Code Online (Sandbox Code Playgroud)


Jos*_*nga 5

$items = SomeDataModel->get()->paginate(2); // in your controller

@foreach(items as $item)   // in your view.blade file
....echo some data here
@endforeach

<div class="pagination">
    {{ $items->render() }} or {{ $items->links() }}
</div>
Run Code Online (Sandbox Code Playgroud)

在 render() 或 links() 方法中使用数组名称(项目)而不是数组项目。这对我有用。