繁体   English   中英

使用关系和paginate()时使用laravel获取数据的问题

[英]problem with fetching data with laravel when i use relations and paginate()

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

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

使用->paginate(10)或> 5不提供任何数据并在控制台上使用Vue js获得错误但响应正常200我使用这个应用程序而不使用vujs我使用laravel从3年,抱歉dd()和postman和所有使用的东西都完成了给我的对象json命名results { 0{} 1{} 2{} }所有工作

几点建议

对于分页,我建议你使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM