Laravel 5.4数据表插件

问题描述:

我想使用yajara-laravel-databaseLaravel 5.4数据表插件

这是我的控制器

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行数据,我通过在HTML视图中放置{{$ posts}}进行了验证。

的{{$帖子}}

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":[]} 

我试图把{{$后> ID}} H​​TML视图,并得到这个错误

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

试过代码{{$ }

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

什么是填充数据的过程。该wiki网址无效

使用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', '[email protected]')->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> 

下面是Docs