2017-02-26 94 views
0

我想使用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網址無效

回答

0

使用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