Reports
@if ($reports->count())| Status | Info | Date |
|---|---|---|
| {{ $report['status'] }} | {{ $report['info'] }} | {{ $report['created_at'] }} |
No records exist.
@endif {!! $reports->render() !!}I would like to paginate the following relationship (a Category having many Apps):
class Category extends Model
{
To get this to work, I had to bypass the Eloquent Relationships. I created a repository instead.
In this example, a user has lots of reports.
App\Repositories\ReportsRepository
This will get the reports records for a user.
namespace App\Repositories;
use App\User;
class ReportsRepository
{
public function findByUser(User $user, $paginate = 10)
{
$reports = User::find($user->id)
->reports()
->orderBy('created_at', 'DESC')
->paginate($paginate);
return $reports;
}
}
ReportController
Here we call the ReportsRepositroy to get the records (I've removed the Auth code).
class ReportController extends Controller
{
public function index(\App\Repositories\ReportsRepository $repo)
{
$reports = $repo->findByUser(Auth::user());
return view('report.index', ['reports' => $reports]);
}
}
View - report/index.blade.php
The important bit for pagination here is {!! $reports->render() !!}. This generates the links of the pagination.
@extends('layout.master')
@section('content')
Reports
@if ($reports->count())
Status
Info
Date
@foreach($reports as $report)
{{ $report['status'] }}
{{ $report['info'] }}
{{ $report['created_at'] }}
@endforeach
@else
No records exist.
@endif
{!! $reports->render() !!}
@stop
This is all that's needed. Laravel deals with the rest of the pagination magic itself.
Hope this helps.