duanli0162 2019-06-05 12:23
浏览 393

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

what is error when i want to fetching data with paginate(10) the Vue js dosn't do it but when i use paginate(5) it working good this code of Controller with relationship in model files and the response status 200 ok working

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

this code is actually worked for me but i want to make 10 results in my page Like this

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

with ->paginate(10) or > 5 not giving any data and get error on console with Vue js but the response is ok 200 i make like this application without using vujs i used laravel from 3 years , sorry dd() and postman and all things used is done giving me the object json named results { 0{} 1{} 2{} } all working

  • 写回答

1条回答 默认 最新

  • dtp0760 2019-06-05 12:56
    关注

    SUGGESTIONS

    for pagination i will suggest you use jquery datatable for proper pagination. Its quite okay and saves lots of time. see below the sample implementation:

    //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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能