【问题标题】:problem with fetching data with laravel when i use relations and paginate()当我使用关系和分页()时,使用 laravel 获取数据的问题
【发布时间】:2019-10-20 23:04:27
【问题描述】:

当我想用 paginate(10) 获取数据时出现什么错误,Vue js 没有这样做,但是当我使用 paginate(5) 时,它的控制器代码运行良好,与模型文件和响应状态 200 ok working

$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 的情况下制作了这个应用程序,我使用了 3 年的 laravel,抱歉 dd() 和邮递员,所有使用的东西都完成了,给我一个名为 results { 0{} 1{} 2{} } 的对象 json 一切正常

【问题讨论】:

  • 错误是什么?
  • 您实际上有多少数据用于此查询?也放你的 vuejs 代码
  • 数据库中有25条记录,用axios取数据后报错,找不到post.comment.text
  • 确保所有帖子都有评论。可能前 5 个都有comment,但接下来的几个没有。如果post.comment.text 出现错误,则可能是commentnull,然后在尝试从comment 获取text 时会出现错误。
  • Teun :: 你是主人这是我的错,非常感谢

标签: javascript php laravel vue.js


【解决方案1】:

建议

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

【讨论】:

  • 谢谢你回答我,但我使用 Vuejs 和 axios 我的问题不是让分页页面使用 axios 获取数据的问题都记录了它的正常显示,并且响应我有我所有的数据 json 但是当我尝试获取 post.comment.text 时,在 vue not found post.comment.text 上出现错误
猜你喜欢
  • 1970-01-01
  • 2021-02-09
  • 2019-10-25
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 2018-01-11
相关资源
最近更新 更多