Laravel-分页链接未出现 [英] Laravel - pagination links not appearing

查看:93
本文介绍了Laravel-分页链接未出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,{{$ items-> links()}}什么也没做.

此问题的直接原因是$ items-> getLastPage()返回0.相同的$ items-> getTotal().

我可以在url中更改页面参数,它可以正常工作-可以转到正确的页面. 但是$ items-> getCurrentPage()在每个页面上返回1.

分页对我来说与其他模型完美搭配,只是这给我带来了麻烦.

当我手动创建分页器时,它似乎也可以正常工作,但是我希望能够从模型中访问某些方法,所以我想使用Eloquent模型而不是原始数组.

代码:

$records = EventLog::byObject($model, $id)->paginate(10);

 static public function byObject($model, $id)
       {

           $records = DB::select([query], [params]);
           return self::getHistory($records, $model);
       }

 static public function getHistory($records, $model = '')
       {
           $ids = array();
            foreach ($records as $record) {
                array_push($ids, (int)$record->id);
            }

            $history = array();                

            if (count($ids)) {

                $history = EventLog::whereRaw('id IN (' . implode(',', array_fill(0, count($ids), '?')) . ')', $ids)
                ->with('creator')
                        ->with(array('eventfields' => function($query)
                        {
                            $query->whereRaw('field NOT LIKE \'%_id\'');
                        }))
                        ->orderBy('event_logs.created_at')
                        ->orderByRaw('(CASE WHEN eventable_type=? THEN 0 ELSE 1 END)', array($model))
                        ->orderByRaw('(CASE WHEN parent_type=? THEN 0 ELSE 1 END)', array($model));

            }
            return $history;
       }

返回的$ history具有正确的类型,可以正确显示,但是由于某种原因分页不起作用.

我正在尝试使用显示链接

{{ $records->links(); }}

谢谢

解决方案

我在这里遇到了同样的问题,laravel 4.1和带有全文搜索的查询范围.就我而言,问题是 orderByRaw (正常的orderby不会中断分页链接).

所以我的差劲"解决方案是在需要分页并使用时关闭 orderByRaw :

->orderBy(DB::raw('my raw orderby'));

有效

For some reason {{ $items->links() }} doesn't do anything.

Direct cause of this problem is that $items->getLastPage() returns 0. Same $items->getTotal().

I can change page parameter in the url and it works fine - it goes to correct page. however $items->getCurrentPage() returns 1 on each page.

Pagination works perfectly for me with other models just this one gives me problems.

Also it seems to be working fine when I create the paginator manually but I want to be able access some methods from the model so I want to use Eloquent models and not raw array.

Edited: Code:

$records = EventLog::byObject($model, $id)->paginate(10);

 static public function byObject($model, $id)
       {

           $records = DB::select([query], [params]);
           return self::getHistory($records, $model);
       }

 static public function getHistory($records, $model = '')
       {
           $ids = array();
            foreach ($records as $record) {
                array_push($ids, (int)$record->id);
            }

            $history = array();                

            if (count($ids)) {

                $history = EventLog::whereRaw('id IN (' . implode(',', array_fill(0, count($ids), '?')) . ')', $ids)
                ->with('creator')
                        ->with(array('eventfields' => function($query)
                        {
                            $query->whereRaw('field NOT LIKE \'%_id\'');
                        }))
                        ->orderBy('event_logs.created_at')
                        ->orderByRaw('(CASE WHEN eventable_type=? THEN 0 ELSE 1 END)', array($model))
                        ->orderByRaw('(CASE WHEN parent_type=? THEN 0 ELSE 1 END)', array($model));

            }
            return $history;
       }

The returned $history has the right type, is displayed correctly but the pagination is not working for some reason.

I am trying to display links using

{{ $records->links(); }}

Thanks

解决方案

I've same problem here, laravel 4.1 and a query scope with fulltext search. In my case the problem was the orderByRaw (a normal orderby doesn't broke pagination links).

So my "poor" solution is to keep orderByRaw off when I need a pagination and use:

->orderBy(DB::raw('my raw orderby'));

that works

这篇关于Laravel-分页链接未出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆