如何在Laravel中进行分页?

问题描述 投票:0回答:2

所以我想要的是在我的视图中使用paginate,但是我总是遇到一个错误: 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();}}

所以,如果有人有什么想法,我会很感激

php twitter-bootstrap laravel pagination laravel-4
2个回答
3
投票

使用分页时无需使用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);

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()
© www.soinside.com 2019 - 2024. All rights reserved.