NOTE. This did not use the Chumper / Datatable package, but it uses jQuery datatables, so it can be useful.
Here is how I did it. It was a scenario in which I had a table with American football teams. Each team participated in the Conference, which was part of the Division. Teams can be sorted by team name, conference, or department. The following is the server code used to implement it. In addition, they can be filtered by a conference or unit.
/* * Route::get( 'api/v1/teams-table', ' APIController@teamsTable ' ); */ public function dataTable() { // get the input parameters $i = Input::all(); // parse the parameters and set default values $draw = isset( $i[ 'draw' ] ) ? $i[ 'draw' ] : 1; $start = isset( $i[ 'start' ] ) ? $i[ 'start' ] : 0; $length = isset( $i[ 'length' ] ) ? $i[ 'length' ] : 10; $search = isset( $i[ 'search' ][ 'value' ] ) && '' != $i[ 'search' ][ 'value' ] ? $i[ 'search' ][ 'value' ] : false; $ordrby = isset( $i[ 'order' ] ) ? $i[ 'columns' ][ $i[ 'order' ][ 0 ][ 'column' ] ][ 'name' ] : ''; $ordrdr = isset( $i[ 'order' ] ) ? $i[ 'order' ][ 0 ][ 'dir' ] : 'asc'; $total = Team::count(); $filter = $total; // get the data if ( '' == $search ) { switch( $ordrby ) { case 'name': $teams = Team::with( 'conferences', 'logo', 'conferences.division' ) ->skip( $start ) ->take( $length ) ->orderBy( 'name', $ordrdr ) ->get(); break; case 'conference': $teams = Team::with( 'conferences', 'logo', 'conferences.division' ) ->join( 'conference_team', 'conference_team.team_id', '=', 'teams.id' )->join( 'conferences', 'conferences.id', '=', 'conference_team.conference_id' ) ->orderBy( 'conferences.abbr', $ordrdr ) ->skip( $start ) ->take( $length ) ->get(); break; case 'division': $teams = Team::with( 'conferences', 'logo', 'conferences.division' ) ->skip( $start ) ->take( $length ) ->conference() ->division() ->orderBy( 'abbr', $ordrdr ) ->get(); break; default: $teams = Team::with([ 'conferences', 'logo', 'conferences.division' ]) ->skip( $start ) ->take( $length ) ->get(); } } else { $teams = Team::with( 'conferences', 'logo', 'conferences.division' ) ->skip( $start ) ->take( $length ) ->where( 'name', 'LIKE', '%' . $search . '%' ) ->orWhereHas( 'conferences', function( $q ) use ( $search ) { $q->where( 'abbr', 'LIKE', '%' . $search . '%' ) ->orWhereHas( 'division', function( $qu ) use ( $search ) { $qu->where( 'abbr', 'LIKE', '%' . $search . '%' ); }); }) ->get(); $filter = Team::with( 'conferences', 'logo', 'conferences.division' ) ->where( 'name', 'LIKE', '%' . $search . '%' ) ->orWhereHas( 'conferences', function( $q ) use ( $search ) { $q->where( 'abbr', 'LIKE', '%' . $search . '%' ) ->orWhereHas( 'division', function( $qu ) use ( $search ) { $qu->where( 'abbr', 'LIKE', '%' . $search . '%' ); }); }) ->count(); } // loop through the retrieved data and format it to be returned as JSON $data = []; foreach ( $teams as $t ) { $show = URL::route( 'admin.team.show', $t->slug ); $edit = URL::route( 'admin.team.depth_chart', $t->slug ); $data[] = [ 'checkbox' => '<label><input type="checkbox" class="ace" value="' . $t->id . '" /><span class="lbl"></span></label>', 'logo' => '<img src="' . $t->logo->filename . '" alt="' . $t->name . ' logo" height="40">', 'name' => [ 'display' => link_to_route( 'admin.team.show', $t->name, [ $t->slug ] ), 'filter' => $t->name, 'sort' => $t->name, ], 'conference' => [ 'display' => link_to_route( 'admin.conference.show', $t->conferences[ 0 ]->abbr, [ $t->conferences[ 0 ]->slug ] ), 'filter' => $t->conferences[ 0 ]->name . ' ' . $t->conferences[ 0 ]->abbr, 'sort' => $t->conferences[ 0 ]->abbr, ], 'division' => [ 'display' => link_to_route( 'admin.division.show', $t->conferences[ 0 ]->division->abbr, [ $t->conferences[ 0 ]->division->slug ] ), 'filter' => $t->conferences[ 0 ]->division->name . ' ' . $t->conferences[ 0 ]->division->abbr, 'sort' => $t->conferences[ 0 ]->division->abbr, ], 'site' => '<a target="_blank" href="' . $t->url . '">website <i class="fa fa-external-link"></i></a>', 'actions' => sprintf( $this->actions, $show, $edit, $show, $edit ), ]; } $tdata = [ 'draw' => $draw, 'recordsTotal' => $total, //consider caching or setting fixed value for this 'recordsFiltered' => $filter, 'data' => $data, ]; return Response::json( $tdata ); }
In any case, you can customize this example to suit your situation. Hope this helps!