简体   繁体   English

如何在 laravel&Vue 中对数据进行分页?

[英]How can I paginate data in laravel&Vue?

I am making site with laravel and vuejs.我正在使用 laravel 和 vuejs 制作网站。 I use complex Eloquent query for getting data from DB as follows.我使用复杂的 Eloquent 查询从 DB 获取数据,如下所示。

$query->with([
        'deviceAssignment.deviceSetting.sim',
        'deviceAssignment.deviceSetting.device.deviceType',
        'deviceSignal',
        'deviceAssignment.deviceSetting.deviceGroup',
        'deviceAssignment.deviceSetting.company',
        'deviceAssignment.deviceSetting.phones'])
        ->orderBy('created_at', 'desc')
        ->get();

I use several tables at once, then this size of data is somewhat huge, so I want to paginate this data.我一次使用了几个表,那么这个数据量有点大,所以我想对这个数据进行分页。 I placed paginator in the bottom of vue component like this.我像这样将分页器放在 vue 组件的底部。

....... …………

<pager :items="histories" v-on:paginate="changePage" class="pager bottom_10 top_20 position_r"></pager>

And changePage function is as follows;而changePage function如下;

methods: {
        changePage: function(page){
            location.href = "/history/setting?page=" + page;
        },

In this status, simply, I change get() function to paginate() in the first part to realize pagination.在这种状态下,简单来说,我在第一部分将get() function 改为 paginate() 来实现分页。 Am I right?我对吗?

For Pagination use paginate(10) function instead of get() function.对于分页,使用paginate(10) function 代替get() function。

$query->with([
        'deviceAssignment.deviceSetting.sim',
        'deviceAssignment.deviceSetting.device.deviceType',
        'deviceSignal',
        'deviceAssignment.deviceSetting.deviceGroup',
        'deviceAssignment.deviceSetting.company',
        'deviceAssignment.deviceSetting.phones'])
        ->orderBy('created_at', 'desc')
        ->paginate(10);

https://laravel.com/docs/8.x/pagination#paginating-query-builder-results https://laravel.com/docs/8.x/pagination#paginating-eloquent-results https://laravel.com/docs/8.x/pagination#paginating-query-builder-results https://laravel.com/docs/8.x/pagination#paginating-eloquent-results

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

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