【问题标题】:How to do paginate in Laravel?如何在 Laravel 中进行分页?
【发布时间】:2014-05-08 21:22:29
【问题描述】:

所以我想要在我的视图中使用分页,但我总是收到错误Call to a member function paginate() on a non-object,这是我的代码:

首先我的行动:

 public function index()
    {
        $logs = DB::table('services')
                ->join('logs', function($join)
            {
                $join->on('services.id', '=', 'logs.service_id');
            })
            ->get()->paginate(10); //here i got the error

        return View::make('logs.index',array('logs'=> $logs,'title'=>'Service Logs'));
    }

那么我的看法:

<table class="table table-striped table-bordered">
<thead>
    <tr>
        <td>Domain</td>
        <td>Port</td>
        <td>Checktime</td>
        <td>Status</td>
        <td>Responce</td>
    </tr>
</thead>
<div class="container">
@foreach($logs as $key => $value)
    <tr>
        <td>{{ $value->domain }}</td>
        <td>{{ $value->service_port }}</td>
        <td>{{ $value->checktime }}</td>
        <td class="text-center">
            @if( $value->status == 'up' ) <img src="../img/up3.png" />
            @elseif( $value->status == 'down' ) <img src="../img/down3.png" />
            @else <img width="30" height="30" src="../img/warning_icon.png" />
            @endif
        </td>
        <td>{{ $value->response_time }}</td>
    </tr>
@endforeach
   </div>
   </table>
   {{$logs->links();}}

所以如果有人有任何想法,我会非常感激

【问题讨论】:

  • 你得到什么错误?
  • 在非对象上调用成员函数 paginate()

标签: php twitter-bootstrap laravel pagination laravel-4


【解决方案1】:

使用分页时不需要使用get。您的查询应该是:

$logs = DB::table('services')->join('logs', function($join)
        {
            $join->on('services.id', '=', 'logs.service_id');
        })->paginate(10);

事实上,您也可以删除该闭包,因为这确实不是一个复杂的连接。

$logs = DB::table('services')
          ->join('logs', 'services.id', '=', 'logs.service_id')
          ->paginate(10);

【讨论】:

  • 投票 +1,我知道我无法与 Jason Lewis 竞争!感谢您为 Laravel 社区所做的工作
  • 感谢 Jason,它可以工作,但我怎样才能在我的视图中获得分页?
【解决方案2】:

从文档 (http://laravel.com/docs/pagination) 看来,正确的用法是:

$logs = DB::table('services')
    ->join('logs', function($join)
    {
        $join->on('services.id', '=', 'logs.service_id');
    })
    ->paginate(10); //removed get()

【讨论】:

    猜你喜欢
    • 2021-09-27
    • 2018-09-04
    • 2020-03-07
    • 2013-08-24
    • 2019-10-02
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多