Skip to content
Arjay Angeles edited this page May 20, 2016 · 2 revisions

###Check Laravel Datatables Demo App for a full pack of examples and see it in action.

Example 1:

    $posts = Post::select(array('posts.id','posts.name','posts.created_at','posts.status'));

    return Datatables::of($posts)->make();

Example 2:

    $place = Place::leftJoin('owner','places.author_id','=','owner.id')
                    ->select(array('places.id','places.name','places.created_at','owner.name as ownername','places.status'));


    return Datatables::of($place)
    ->addColumn('operations','<a href="{{ URL::route( \'admin.post\', array( \'edit\',$id )) }}">edit</a>
                    <a href="{{ URL::route( \'admin.post\', array( \'delete\',$id )) }}">delete</a>
                ')
    ->editColumn('status','@if($status)
                                Active
                            @else
                                Passive
                            @endif')
    // you can also give a function as parameter to editColumn and addColumn instead of blade string
    ->editColumn('ownername','Author of this post is {{ $ownername }}')
    ->removeColumn('id')
    ->make();

Example 3: Overriding default filter option

    $posts = Post::select(array('posts.id','posts.name','posts.created_at','posts.status'));

    return Datatables::of($posts)
        ->filter(function($query){
            if (Input::get('id')) {
                $query->where('id','=',Input::get('id'));
            }
        })->make();

Example 4: Accessing Carbon object on timestamps and/or any objects in model

Note: Only applicable if you use Eloquent object.

    $posts = Post::select(array('posts.id','posts.name','posts.created_at','posts.status'));

    return Datatables::of($posts)
        ->editColumn('created_at', function($data){ return $data->created_at->toDateTimeString() })
        ->filter(function($query){
            if (Input::get('id')) {
                $query->where('id','=',Input::get('id'));
            }
        })->make();

Example 5: Returning object data source

    $posts = Post::select(array('posts.id','posts.name','posts.created_at','posts.status'));

    return Datatables::of($posts)->make(true);

Example 6: DT_RowId, DT_RowClass, DT_RowData and DT_RowAttr

$users = User::select('*');

return Datatables::of($users)
    ->setRowId('id') // via column name if exists else just return the value
    ->setRowId(function($user) {
        return $user->id;
    }) // via closure
    ->setRowId('{{ $id }}') // via blade parsing

    ->setRowClass('id') // via column name if exists else just return the value
    ->setRowClass(function($user) {
        return $user->id;
    }) // via closure
    ->setRowClass('{{ $id }}') // via blade parsing

    ->setRowData([
        'string' => 'data',
        'closure' => function($user) {
            return $user->name;
        },
        'blade' => '{{ $name }}'
        ])
    ->addRowData('a_string', 'value')
    ->addRowData('a_closure', function($user) {
        return $user->name;
    })
    ->addRowData('a_blade', '{{ $name }}')
    ->setRowAttr([
        'color' => 'data',
        'closure' => function($user) {
            return $user->name;
        },
        'blade' => '{{ $name }}'
        ])
    ->addRowAttr('a_string', 'value')
    ->addRowAttr('a_closure', function($user) {
        return $user->name;
    })
    ->addRowAttr('a_blade', '{{ $name }}')
    ->make(true);

###Example View and Controller On your view:

<table id="users" class="table table-hover table-condensed">
    <thead>
        <tr>
            <th class="col-md-3">{{{ Lang::get('users/table.username') }}}</th>
            <th class="col-md-3">{{{ Lang::get('users/table.email') }}}</th>
            <th class="col-md-3">{{{ Lang::get('users/table.created_at') }}}</th>
            <th class="col-md-3">{{{ Lang::get('table.actions') }}}</th>
        </tr>
    </thead>
</table>

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

On your controller:

public function getData()
{
    $users = $this->users->select('*');

    return Datatables::of($users)
        ->addColumn('action', 'action here')
        ->make(true);
}