0

我正在尝试使用yajara-laravel-database

这是我的控制器

public function index(Request $request){
    $posts = Datatables::eloquent(Posts::query())->make(true);
    return View::make('dashboard.approval', compact('posts'));
}

这是我的观点

    <table class="ui celled table">
  <thead>
    <tr><th>id</th>
    <th>Title</th>
    <th>Description</th>
  </tr></thead>
  <tbody>
  @foreach($posts as $post)
    <tr>
        <td>lsdjflajsdlk</td>
        <td>Hello</td>
        <td>Hello</td>
    </tr>
    @endforeach
  </tbody>

</table>
@endsection

这是我的脚本标签

<script>
   $(document).ready(function(){
          $('.table').DataTable({

            });
        });
      </script>

我正在获取数据表结构。但目前我只得到 3 行,但我有 7 行数据,我通过将 {{$posts}} 放在 html 视图中进行验证。

{{$posts}} 的 HTML 视图

HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Type: application/json {"draw":0,"recordsTotal":7,"recordsFiltered":7,"data":[{"id":"1",".............],"queries":[{"query":"select count(*) as aggregate from (select '1' as `row_count` from `posts`) count_row_table","bindings":[],"time":70.87},{"query":"select * from `posts`","bindings":[],"time":2.22}],"input":[]}

我尝试输入 {{$post->id}} 并收到此错误

Undefined property: Symfony\Component\HttpFoundation\ResponseHeaderBag::$id

试过的代码{{$post}}

htmlspecialchars() expects parameter 1 to be string, array given 

填充数据的过程是什么。wiki 网址不起作用

4

1 回答 1

1

使用 yajra-datatables 填充数据不是这样工作的。When you use Yajra-Datatables it returns the data in Json format and we have to populate it using jquery datatables.

请按照以下步骤操作:

使用 yajra-datables 制作返回数据的新方法

//this will return the data in json form
public function getPosts() 
{
    return Datatables::eloquent(Posts::query())->make(true);

}

现在为此制定路线

//You can check the response by hitting this route
Route::get('getPosts', 'PostController@getPosts' )->name('get.posts');

您的视图不应有以下几行,

 <tbody>
  @foreach($posts as $post)
    <tr>
        <td>lsdjflajsdlk</td>
        <td>Hello</td>
        <td>Hello</td>
    </tr>
    @endforeach
 </tbody>

这就是我们填充数据的方式

//This is how we populate the data
<script type="text/javascript">
    $(document).ready(function(){
    $('.table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{!! route('get.posts') !!}',

        //You will have to give the column names of the table.
        columns: [
            { data: 'name', name: 'name' },
            { data: 'phone', name: 'phone' },
            { data: 'message', name: 'message' },

        ]
    });
});
</script>

这是文档

于 2017-05-18T14:33:04.060 回答