使用 Laravel 和 with() 进行分页

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


我希望该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: 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_url, per_page, total, next_page_url, 等等...


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


慕的地10843
浏览 383回答 3
3回答

Smart猫小萌

所以你想对你的关系进行分页,如果你有好的模型,我认为你可以这样做:public function index()&nbsp; {&nbsp; &nbsp; $data = Category::where([&nbsp; &nbsp; &nbsp; ['slug', '=', 'interview-questions'],&nbsp; &nbsp; &nbsp; ['lang', '=', auth()->user()->lang],&nbsp; &nbsp; &nbsp; ['profile_id', '=', auth()->user()->profile_id]&nbsp; &nbsp; ])->with(&nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'questions' => function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->whereNull('parent_id')->with(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'answer' => function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->where('user_id', auth()->user()->id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )->orderBy('position', 'ASC');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; )->first();&nbsp; &nbsp; $data = $data->questions()->paginate(15);}或者,您可以单独对关系数据进行分页并将其作为新变量发送:$questions = $data->questions()->paginate(15);如果您有任何错误,请在此处提供。

慕慕森

那是因为您在错误的地方使用了分页,并且您也在使用first()- 它只获取它从过滤器中找到的第一个数据。将您的代码更改为:&nbsp;public function index(){&nbsp; &nbsp; $data = Category::where([&nbsp; &nbsp; &nbsp; ['slug', '=', 'interview-questions'],&nbsp; &nbsp; &nbsp; ['lang', '=', auth()->user()->lang],&nbsp; &nbsp; &nbsp; ['profile_id', '=', auth()->user()->profile_id]&nbsp; &nbsp; ])->with(&nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'questions' => function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->whereNull('parent_id')->with(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'answer' => function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->where('user_id', auth()->user()->id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )->orderBy('position', 'ASC');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; )->paginate(15);}未经测试,但应该给你想要的结果。如果您有任何错误,请告诉我。

HUH函数

您不能使用带有分页的first()或get()方法,因为分页本身会获取您作为参数给出的结果数(限制)。即对于您的情况,它将获取前 15 个结果并相应地分页。在访问其他页面时,它只是添加了限制的偏移量。下面是一个示例代码。注意:分页将应用于类别,而不是问题。public function index(){&nbsp; &nbsp; $data = Category::where([&nbsp; &nbsp; &nbsp; &nbsp; ['slug', '=', 'interview-questions'],&nbsp; &nbsp; &nbsp; &nbsp; ['lang', '=', auth()->user()->lang],&nbsp; &nbsp; &nbsp; &nbsp; ['profile_id', '=', auth()->user()->profile_id]&nbsp; &nbsp; ])->with([&nbsp; &nbsp; &nbsp; &nbsp; 'questions' => function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->whereNull('parent_id')->with([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'answer' => function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->where('user_id', auth()->user()->id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ])->orderBy('position', 'ASC');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ])->paginate(15);}
打开App,查看更多内容
随时随地看视频慕课网APP