自定义 Laravel 查询结果分页中显示的链接数量

问题描述 投票:0回答:1

我在应用程序中的查询中使用 Laravel 的 Pagination 功能,该功能作为 JSON 响应返回到我的前端(它是 Laravel Rest API)

/**
 * Applications
 */
public function getApplications()
{

    $applications = Application::orderBy('created_at', 'desc')
                               ->paginate(25);

    foreach ($applications as $key => $applicant) {
      try {
        $applications[$key]['data'] = json_decode($applicant['data']);
      } catch (\Exception $e) { }
    }

    return $applications;

}

/**
 * All applications
 *
 * @param  Request  $request
 */
public function applications(Request $request)
{

    try {

      // daily graphs
      $applications = $this->getApplications();

      // not set or nothing to report
      if (!$applications) {
        return response()->json([
          'success' => false,
          'msg' => 'No applications found, check back in a few minutes',
        ], 422);
      }

      // return the response
      return response()->json([
        'success' => true,
        'msg' => 'Icicle applications',
        'applications' => $applications ?? null
      ], 200);

    } catch (\Exception $e) {

      // return default template
      return response()->json([
        'success' => false,
        'msg' => 'We were unable to load applications right now',
      ], 422);

    }

}

但是,页面上显示的默认链接数量从 1 开始,然后增加到 10,这对我来说太多了,我想减少它们以显示更少的链接,例如,1 到 5。

我似乎无法在这里找到任何有关如何配置分页器的文档,并且由于它不是视图,因此如何减少链接?我是否缺少一些配置?

php laravel pagination nuxt.js laravel-pagination
1个回答
3
投票

文档建议在刀片模板内执行此操作:

{{ $users->onEachSide(5)->links() }}

如果您使用客户端模板,则可以使用此:

/**
 * Applications
 */
public function getApplications()
{

    $paginator = Application::orderBy('created_at', 'desc')
                               ->paginate(25);

    return array_merge($paginator, [
        'pages' => $paginator->getUrlRange(1, 10)
    ]);
}
© www.soinside.com 2019 - 2024. All rights reserved.