首页 > 解决方案 > 当我使用关系和分页()时,使用 laravel 获取数据的问题

问题描述

当我想使用paginate(10)获取数据时出现什么错误,Vue js 不会这样做但是当我使用 paginate(5) 时它运行良好,这个控制器代码与模型文件中的关系和响应状态 200 正常工作

$results = Posts::with(['comment'])
            ->orderBy('created_at', 'desc')
            ->paginate(5);
        return response()
            ->json(['results' => $results]);  

这段代码实际上对我有用,但我想在我的页面中生成 10 个结果 像这样

$results = Posts::with(['comment'])
->orderBy('created_at', 'desc')
->paginate(10);
return response()
->json(['results' => $results]); 

或 > 5 不提供->paginate(10)任何数据并在使用 Vue js 的控制台上出现错误但响应正常 200 我在不使用 vujs 的情况下制作了这个应用程序 我使用了 3 年的 laravel ,对不起dd(),邮递员,所有使用的东西都完成了给我对象json命名results { 0{} 1{} 2{} }所有工作

标签: javascriptphplaravelvue.js

解决方案


建议

对于分页,我建议您使用 jquery 数据表进行正确的分页。它很好,可以节省很多时间。请参见下面的示例实现:

//this section call the document ready event making sure that datatable is loaded
<script>

 $(document).ready(function() {
    $('#').DataTable();
  } );

//this section display the datatable
  $(document).ready(function() {
      $('#mytable').DataTable( {
          dom: 'Bfrtip',
          "pageLength": 10, //here you can set the page row number limit
          buttons: [
              {
                  extend: 'print',
                  customize: function ( win ) {
                      $(win.document.body)
                          .css( 'font-size', '10pt' )
                          .prepend(
                              ''
                          );

                      $(win.document.body).find( 'table' )
                          .addClass( 'compact' )
                          .css( 'font-size', 'inherit' );
                  }
              }
          ]
      } );
  } );
</script>

//you can display record on the datatable after querying from your cntroller as shown below
<div class="table-responsive col-md-12">
 <table id="mytable" class="table table-bordered table-striped table-highlight">
                                            <thead>
                                              <tr bgcolor="#c7c7c7">
                                                <th>S/N</th>
                                                 <th>Name</th>
                                              </tr>
                                            </thead>

                                            <tbody>

                                              @php
                                              $i=1;
                                              @endphp
                                                @foreach($queryrecord as $list)

                                                   <tr>
                                                   <td>{{ $i++ }}</td>
                                                   <td>{{ $list->name }}</td>
                                                   </tr>
                                               @endforeach
                                                </tbody>

                                          </table>
                                           <hr />
                                        </div>

推荐阅读