为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Laravel 和 with() 进行分页

使用 Laravel 和 with() 进行分页

PHP
慕的地10843 2021-12-03 15:58:36
我有一个雄辩的要求,要拿起我的类别和相关问题。我希望该questions部分有一个分页。  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();}这是我得到的:data: {id: 31, title: "Interview - Questions", slug: "interview-questions", part: "interview-questions",…}created_at: "2019-08-22 16:28:33"deleted_at: nullid: 31lang: "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_url, per_page, total, next_page_url, 等等...为什么 ?以及如何解决这个问题?
查看完整描述

3 回答

?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

所以你想对你的关系进行分页,如果你有好的模型,我认为你可以这样做:


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);

}

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


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

如果您有任何错误,请在此处提供。


查看完整回答
反对 回复 2021-12-03
?
慕慕森

TA贡献1856条经验 获得超17个赞

那是因为您在错误的地方使用了分页,并且您也在使用first()- 它只获取它从过滤器中找到的第一个数据。将您的代码更改为:


 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);

}

未经测试,但应该给你想要的结果。如果您有任何错误,请告诉我。


查看完整回答
反对 回复 2021-12-03
?
HUH函数

TA贡献1836条经验 获得超4个赞

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


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


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);

}


查看完整回答
反对 回复 2021-12-03
  • 3 回答
  • 0 关注
  • 382 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信