Laravel 上一页和下一页进入控制器

Laravel previous and next page into controller

我正在使用这个 Laravel SEO 包 artesaos/seotools。

有没有办法在控制器中获取上一页和下一页进行分页?

function seoMetaDetail($detail)
{
    SEOMeta::setCanonical(request()->fullUrl());         
    SEOMeta::setPrev(''); <-- previous page
    SEOMeta::setNext(''); <-- next page
}

您可以从查询字符串中获取当前页面。

function seoMetaDetail($detail)
{
    $current = request()->query('page');

    SEOMeta::setCanonical(request()->fullUrl());
    // If there's no current, then we're on page 1. return null. Otherwise current - 1
    SEOMeta::setPrev($current ? ($current - 1) : null);
    // If there's no current, then we're on page 1, return 2. Otherwise current + 1
    SEOMeta::setNext($current ? ($current + 1) : 2);

}

您也可以使用分页器方法。

public function seoMetaDetail($detail)
{
    $paginator = YourModel::query()->....->paginate();

    SEOMeta::setCanonical(request()->fullUrl());
    SEOMeta::setPrev($paginator->previousPageUrl());
    SEOMeta::setNext($paginator->nextPageUrl());
}

https://laravel.com/docs/9.x/pagination#paginator-instance-methods