首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Laravel和()分页

用Laravel和()分页
EN

Stack Overflow用户
提问于 2019-08-23 10:33:42
回答 3查看 95关注 0票数 2

我有一个雄辩的要求接我的类别和相关的问题。

我希望questions部分有一个分页。

代码语言:javascript
复制
  public function index()
  {
    $data = Category::where([
      ['slug', '=', 'interview-questions'],
      ['lang', '=', auth()->user()->lang],
      ['profile_id', '=', auth()->user()->profile_id]
    ])->with(
      [
        'questions' => function ($query) {
          $query->whereNull('parent_id')->with(
            [
              'answer' => function ($query) {
                $query->where('user_id', auth()->user()->id);
              },
            ]
          )->orderBy('position', 'ASC')->paginate(15);
        }
      ]
    )->first();
}

我得到的是:

代码语言:javascript
复制
data: {id: 31, title: "Interview - Questions", slug: "interview-questions", part: "interview-questions",…}
created_at: "2019-08-22 16:28:33"
deleted_at: null
id: 31
lang: "fr"
part: "interview-questions"
profile_id: 1
   questions: [{id: 956, question: "<h3>1. Qu’est-ce qui vous a intéressé dans notre annonce ?&nbsp;</h3>",…},…]
      0: {id: 956, question: "<h3>1. Qu’est-ce qui vous a intéressé dans notre annonce ?&nbsp;</h3>",…}
      1: {id: 957,…}
      2: {id: 958,…}
      3: {id: 959, question: "<h3>4. Depuis combien de temps cherchez-vous du travail&nbsp;?</h3>",…}
      4: {id: 960,…}
      5: {id: 961,…}
      6: {id: 962,…}
      7: {id: 963,…}
      8: {id: 964, question: "<h3>9. Pourquoi avez-vous postulé chez nous&nbsp;(candidature spontanée) ?</h3>",…}
     9: {id: 965,…}
     10: {id: 966, question: "<h3>11. Racontez-moi vos expériences professionnelles.</h3>", type: "wysiwyg",…} 

我的问题限制在10个。只是,我没有像Laravel这样的分页信息,应该提供first_page_urlper_pagetotalnext_page_url等…。

为什么?如何解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-08-23 11:02:27

所以你想把你的关系分页,如果你有好的模型,我想你可以这样做:

代码语言:javascript
复制
public function index()
  {
    $data = Category::where([
      ['slug', '=', 'interview-questions'],
      ['lang', '=', auth()->user()->lang],
      ['profile_id', '=', auth()->user()->profile_id]
    ])->with(
      [
        'questions' => function ($query) {
          $query->whereNull('parent_id')->with(
            [
              'answer' => function ($query) {
                $query->where('user_id', auth()->user()->id);
              },
            ]
          )->orderBy('position', 'ASC');
        }
      ]
    )->first();

    $data = $data->questions()->paginate(15);
}

或者,您可以将关系数据单独分页并将其作为新变量发送:

代码语言:javascript
复制
$questions = $data->questions()->paginate(15);

如果您有任何错误,请在这里提供。

祝好运!

票数 2
EN

Stack Overflow用户

发布于 2019-08-23 10:38:19

这是因为您在错误的地方使用分页,也使用first() -它只从过滤器获得第一个数据。将代码更改为:

代码语言:javascript
复制
 public function index(){
    $data = Category::where([
      ['slug', '=', 'interview-questions'],
      ['lang', '=', auth()->user()->lang],
      ['profile_id', '=', auth()->user()->profile_id]
    ])->with(
      [
        'questions' => function ($query) {
          $query->whereNull('parent_id')->with(
            [
              'answer' => function ($query) {
                $query->where('user_id', auth()->user()->id);
              },
            ]
          )->orderBy('position', 'ASC');
        }
      ]
    )->paginate(15);
}

没有测试,但应该给你想要的结果。如果你有什么错误请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2019-08-23 10:54:01

不能使用将分页本身作为分页的first()get()方法,获取作为参数(限制)给出的结果数。对于您的情况,它将获取前15个结果并相应地分页。在访问其他页面时,它只是添加带有限制的偏移量。

下面是一个示例代码。注:分页将应用于类别,而不是问题。

代码语言:javascript
复制
public function index(){
    $data = Category::where([
        ['slug', '=', 'interview-questions'],
        ['lang', '=', auth()->user()->lang],
        ['profile_id', '=', auth()->user()->profile_id]
    ])->with([
        'questions' => function ($query) {
            $query->whereNull('parent_id')->with([
                'answer' => function ($query) {
                    $query->where('user_id', auth()->user()->id);
                },
            ])->orderBy('position', 'ASC');
        }
    ])->paginate(15);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57624514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档