user3438415
user3438415

Reputation: 165

How to do paginate in Laravel?

so what i want is to use paginate in my view but i got always an error Call to a member function paginate() on a non-object, this is my code :

First my Action :

 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'));
    }

Then my view :

<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();}}

So please if someone has any idea i will be so appreciative

Upvotes: 0

Views: 148

Answers (2)

ilpaijin
ilpaijin

Reputation: 3695

From the docs (http://laravel.com/docs/pagination) it seems that the correct usage is:

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

Upvotes: 2

Jason Lewis
Jason Lewis

Reputation: 18665

You do not need to use get when you are using pagination. Your query should simply be:

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

In fact you can remove that closure as well since this really isn't a complex join.

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

Upvotes: 3

Related Questions