I would like to paginate the following relationship (a Category having many Apps):

class Category extends Model
{
    public function apps()
    {
        return $this->hasMany('App\App')->orderBy('current_price', 'asc');
    }
}

The problem is, when I add ->paginate(10); to the end of that line, I get the following error:

Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

What am I missing here?

shareimprove this question
 
1 
I don't think you can add a paginate method to an relation. Implementing pagination to a relationship. What you'll have to do is paginate the apps variable when returning in controller.. 



2 Answers 正确答案 

Have you tried this?

$category = Category::first();
$apps = $category->apps()->paginate(10);
return view('example', compact('category', 'apps'));

Then, on your view, you can just loop through the apps.

@foreach ($apps as $app)
    {{ $app->id }}
@endforeach

{!! $apps->render() !!}

If you want to set it as a relationship to category, you can use the setRelation method:

$category = Category::first();
$category->setRelation('apps', $category->apps()->paginate(10));
return view('example', compact('category');

Then in your view:

@foreach ($category->apps as $app)
    {{ $app->id }}
@endforeach

{!! $category->apps->render() !!}
shareimprove this answer
 
   
This worked like a charm! It's not eager loading, but it works. Thanks! – cbloss793 Feb 17 at 21:30

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')
    <div class="content">
        <h1>Reports</h1>

        @if ($reports->count())
            <table class="table">
                <thead>
                    <tr>
                        <th>Status</th>
                        <th>Info</th>
                        <th>Date</th>
                    </tr>
                </thead>

                <tbody>
                @foreach($reports as $report)
                    <tr class="{{ $report['status'] }}">
                        <td>{{ $report['status'] }}</td>
                        <td>{{ $report['info'] }}</td>
                        <td>{{ $report['created_at'] }}</td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        @else
            <p>No records exist.</p>
        @endif

        {!! $reports->render() !!}

    </div>
@stop

This is all that's needed. Laravel deals with the rest of the pagination magic itself.

Hope this helps.


来自  https://stackoverflow.com/questions/33507581/how-to-paginate-a-has-many-relationship-that-is-ordered