user5405873
user5405873

Reputation:

How to retrieve 10 record per page rather whole data records using yajra laravel datatable

I have huge records stored in database which is taking time to load (sometime more than a minute).

so i thought about using yajra laravel datatable (https://github.com/yajra/laravel-datatables)

Question: only 10 records has to come from database on click of next button next 10 record has to come.

here is my files:

web.php

Route::get('datatable', ['uses'=>'PostController@datatable']);
Route::get('datatable/getposts', ['as'=>'datatable.getposts','uses'=>'PostController@getPosts']);

controller => PostController.php

public function datatable()
{
    return view('datatable');
}
public function getPosts()
{
    $users = DB::table('data')->select('*');   
    return Datatables::of($users)->make(true);  

}

view => datatable.blade.php

<script type="text/javascript">
$(document).ready(function() {
    oTable = $('#users').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('datatable.getposts') }}",
        "columns": [
            {data: 'id', name: 'id'},
            {data: 'username', name: 'username'},
            {data: 'about', name: 'about'},
            {data: 'created_at', name: 'created_at'}
        ]
    });
}); 
</script>

please help me thanks in advance!!!!!!

Upvotes: 1

Views: 4444

Answers (1)

Exprator
Exprator

Reputation: 27503

<script type="text/javascript">
$(document).ready(function() {
     $('#users').DataTable({
        "pageLength": 20,
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('datatable.getposts') }}",
        "columns": [
            {data: 'id', name: 'id'},
            {data: 'username', name: 'username'},
            {data: 'about', name: 'about'},
            {data: 'created_at', name: 'created_at'}
        ]
    });
}); 
</script>

try this

Upvotes: 2

Related Questions