laravel 如何对有序的“有很多”关系进行分页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33507581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to paginate a "has many" relationship that is ordered?
提问by Saulo Silva
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:
问题是,当我添加->paginate(10);
到该行的末尾时,出现以下错误:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
关系方法必须返回一个 Illuminate\Database\Eloquent\Relations\Relation 类型的对象
What am I missing here?
我在这里错过了什么?
回答by Thomas Kim
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:
如果要将其设置为与类别的关系,可以使用以下setRelation
方法:
$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() !!}
回答by Aine
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
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).
这里我们调用 ReportsRepositroy 来获取记录(我已经删除了 Auth 代码)。
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
查看 - 报告/index.blade.php
The important bit for pagination here is {!! $reports->render() !!}. This generates the links of the pagination.
这里分页的重要部分是{!! $reports->render() !!}。这会生成分页的链接。
@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.
这就是所需要的。Laravel 处理其余的分页魔法本身。
Hope this helps.
希望这可以帮助。