Reputation: 6887
I am trying to use yajara-laravel-database
This is my controller
public function index(Request $request){
$posts = Datatables::eloquent(Posts::query())->make(true);
return View::make('dashboard.approval', compact('posts'));
}
This is my View
<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
This is my script tag
<script>
$(document).ready(function(){
$('.table').DataTable({
});
});
</script>
I am getting the datatable structure. But currently i am getting only 3 rows but i have 7 rows of data and i verified by putting {{$posts}} in html view.
HTML View of {{$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":[]}
I tried putting {{$post->id}} and got this error
Undefined property: Symfony\Component\HttpFoundation\ResponseHeaderBag::$id
Tried code{{$post}}
htmlspecialchars() expects parameter 1 to be string, array given
What is the procedure to populate the data. The wiki urls are not working
Upvotes: 0
Views: 1793
Reputation: 597
Populating data using yajra-datatables does not work like this. When you use Yajra-Datatables it returns the data in Json format and we have to populate it using jquery datatables
.
Please follow these steps:
Make new method for returning the data using yajra-datables
//this will return the data in json form
public function getPosts()
{
return Datatables::eloquent(Posts::query())->make(true);
}
Now make a route for that
//You can check the response by hitting this route
Route::get('getPosts', 'PostController@getPosts' )->name('get.posts');
Your view should not have following lines,
<tbody>
@foreach($posts as $post)
<tr>
<td>lsdjflajsdlk</td>
<td>Hello</td>
<td>Hello</td>
</tr>
@endforeach
</tbody>
This is how we populate data
//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>
Here is Docs
Upvotes: 1