我正在创建一个包含两种类型对象的流,BluePerson和RedPerson。要创建流,我将获取所有两个对象,然后将它们合并到一个集合中。在这样做之后,我需要对它们进行分页,但是paginate用于雄辩的模型和数据库查询,而不是集合。我已经看到很多关于手动创建分页器的内容,但是文档,尤其是API中的文档很稀疏(我甚至无法找到Paginator类接受的参数。)
如何对合并我的馆藏的结果进行分页?
public function index()
{
$bluePerson = BluePerson::all();
$redPerson = RedPerson::all();
$people = $bluePerson->merge($redPerson)->sortByDesc('created_at');
return view('stream.index')->with('people', $people);
}
答案 0 :(得分:27)
如果您想使用LengthAwarePaginator,只需实例化一个。如前一个答案的评论中所述,您必须为此设置路径。您还需要确保解析“currentPage”并在实例化分页器之前设置要返回的项目。这可以在实例化之前/之后完成。所以函数可能看起来像:
function paginateCollection($collection, $perPage, $pageName = 'page', $fragment = null)
{
$currentPage = \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPage($pageName);
$currentPageItems = $collection->slice(($currentPage - 1) * $perPage, $perPage);
parse_str(request()->getQueryString(), $query);
unset($query[$pageName]);
$paginator = new \Illuminate\Pagination\LengthAwarePaginator(
$currentPageItems,
$collection->count(),
$perPage,
$currentPage,
[
'pageName' => $pageName,
'path' => \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPath(),
'query' => $query,
'fragment' => $fragment
]
);
return $paginator;
}
答案 1 :(得分:20)
然而,paginate用于雄辩的模型和数据库查询,而不是集合,似乎。
你是对的。但是对于集合有一个分页器功能。 forPage
Collection forPage(int $page, int $perPage)
休息很简单。
public function foo()
{
$collection = collect([1,2,3,4,5,6,7,8,9,0]);
$items = $collection->forPage($_GET['page'], 5); //Filter the page var
dd($items);
}
答案 2 :(得分:6)
您可以尝试对两个集进行分页并合并它们。您可以在docs和api中找到有关分页的更多信息。以下是手动创建自己的分页器的示例...
$perPage = 20;
$blue = BluePerson::paginate($perPage / 2);
$red = RedPerson::paginate($perPage - count($blue));
$people = PaginationMerger::merge($blue, $red);
我在下面包含了PaginationMerger类。
use Illuminate\Pagination\LengthAwarePaginator;
class PaginationMerger
{
/**
* Merges two pagination instances
*
* @param Illuminate\Pagination\LengthAwarePaginator $collection1
* @param Illuminate\Pagination\LengthAwarePaginator $collection2
* @return Illuminate\Pagination\LengthAwarePaginator
*/
static public function merge(LengthAwarePaginator $collection1, LengthAwarePaginator $collection2)
{
$total = $collection1->total() + $collection2->total();
$perPage = $collection1->perPage() + $collection2->perPage();
$items = array_merge($collection1->items(), $collection2->items());
$paginator = new LengthAwarePaginator($items, $total, $perPage);
return $paginator;
}
}
答案 3 :(得分:4)
您可以在 Providers / AppServiceProvider 中为Collection添加以下代码。
// Enable pagination
if (!Collection::hasMacro('paginate')) {
Collection::macro('paginate',
function ($perPage = 15, $page = null, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
return (new LengthAwarePaginator(
$this->forPage($page, $perPage)->values()->all(), $this->count(), $perPage, $page, $options))
->withPath('');
});
}
然后,您可以像Eloquent模型一样从集合中调用分页。例如
$pages = collect([1, 2, 3, 4, 5, 6, 7, 8, 9])->paginate(5);
答案 4 :(得分:1)
尝试以下操作。
img {
position: absolute;
top: (-$item-height / 2) + ($item-width / 2);
}
答案 5 :(得分:1)
分页收集的最佳方法:
1-将其添加到\ app \ Providers \ AppServiceProvider的启动功能
/*
* use Illuminate\Support\Collection;
* use Illuminate\Pagination\LengthAwarePaginator;
*
* Paginate a standard Laravel Collection.
*
* @param int $perPage
* @param int $total
* @param int $page
* @param string $pageName
* @return array
*/
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
2-从此以后,您可以对所有集合进行分页,就像您的代码一样
$people->paginate(5)
答案 6 :(得分:0)
我必须在一个正在处理的项目中处理类似的问题,在其中的一个页面中,我必须显示两种类型的出版物: 分页 和< strong> 按 created_at 字段排序。在我的情况下,这是一个 Post 模型和一个 Event 模型(以下称为出版物 )。 >
唯一的区别是,我不想从数据库中获取所有出版物,然后合并并对结果进行排序,因为您可以想象,如果我们有数百个出版物,这将引发性能问题。
因此,我发现对每个模型进行分页,然后进行合并和排序会更加方便。
这就是我所做的(基于先前发布的答案和评论)
首先让我向您展示“我的解决方案”的简化版本,然后我将尽我所能解释代码。
use App\Models\Post;
use App\Models\Event;
use App\Facades\Paginator;
class PublicationsController extends Controller
{
/**
* Display a listing of the resource.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$events = Event::latest()->paginate(5);
$posts = Post::latest()->paginate(5);
$publications = Paginator::merge($events, $posts)->sortByDesc('created_at')->get();
return view('publications.index', compact('publications'));
}
}
您可能已经猜到了, 外表分页器 负责对我的分页器进行合并和排序($events
和$posts
)
为了使这个答案更加清晰和完整,我将向您展示如何创建自己的Facade。
您可以选择将自己的外墙放置在您喜欢的任何位置,就像我在树中所示,我个人选择将它们放置在app文件夹下的Facades文件夹内。
+---app
| +---Console
| +---Events
| +---Exceptions
| +---Exports
| +---Facades
| | +---Paginator.php
| | +---...
| +---Http
| | +---Controllers
. . +---...
. . .
将此代码放入app/Facades/Paginator.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Paginator extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'paginator';
}
}
有关更多信息,请参阅How Facades Work
接下来,绑定分页器到服务容器,打开app\Providers\AppServiceProvider.php
namespace App\Providers;
use App\Services\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->app->bind('paginator', function ($app) {
return new Paginator;
});
}
}
有关更多信息,请参阅The Boot Method
我的Paginator类位于app/Services/Pagination/
文件夹下。同样,您可以将您的课堂放在任何喜欢的地方。
namespace App\Services\Pagination;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class Paginator
{
/**
* All of the items being paginated.
*
* @var \Illuminate\Support\Collection
*/
protected $items;
/**
* The number of items to be shown per page.
*
* @var int
*/
protected $perPage;
/**
* The total number of items before slicing.
*
* @var int
*/
protected $total;
/**
* The base path to assign to all URLs.
*
* @var string
*/
protected $path = '/';
/**
* Merge paginator instances
*
* @param mixed $paginators
* @param bool $descending
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
function merge($paginators)
{
$paginators = is_array($paginators) ? $paginators : func_get_args();
foreach ($paginators as $paginator) {
if (!$paginator instanceof LengthAwarePaginator) {
throw new InvalidArgumentException("Only LengthAwarePaginator may be merged.");
}
}
$total = array_reduce($paginators, function($carry, $paginator) {
return $paginator->total();
}, 0);
$perPage = array_reduce($paginators, function($carry, $paginator) {
return $paginator->perPage();
}, 0);
$items = array_map(function($paginator) {
return $paginator->items();
}, $paginators);
$items = Arr::flatten($items);
$items = Collection::make($items);
$this->items = $items;
$this->perPage = $perPage;
$this->total = $total;
return $this;
}
/**
* Sort the collection using the given callback.
*
* @param callable|string $callback
* @param int $options
* @param bool $descending
* @return static
*/
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
{
$this->items = $this->items->sortBy($callback, $options, $descending);
return $this;
}
/**
* Sort the collection in descending order using the given callback.
*
* @param callable|string $callback
* @param int $options
* @return static
*/
public function sortByDesc($callback, $options = SORT_REGULAR)
{
return $this->sortBy($callback, $options, true);
}
/**
* Get paginator
*
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function get()
{
return new LengthAwarePaginator(
$this->items,
$this->total,
$this->perPage,
LengthAwarePaginator::resolveCurrentPage(),
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
]
);
}
}
肯定有改进的空间,因此,如果您发现需要更改的地方,请在此处发表评论或通过twitter与我联系。
答案 7 :(得分:0)
你可以像下面这样改变它:
public function index()
{
$bluePerson = BluePerson::paginate();
$redPerson = RedPerson::all();
$people = $bluePerson->merge($redPerson)->sortByDesc('created_at');
return view('stream.index')->with('people', $people);
}