Skip to content
Arjay Angeles edited this page May 20, 2015 · 1 revision

Usage

It is very simple to use this package. Just create your own fluent query object or eloquent object without getting results (that means don't use get(), all() or similar methods) and give it to Datatables. You are free to use all Eloquent ORM and Fluent Query Builder features.

It is better, you know these:

  • When you use select method on Eloquent or Fluent Query Builder, you choose columns

  • You can easily edit columns by using editColumn($column, $content)

  • You can remove any column by using removeColumn($column) method

  • You can add columns by using addColumn($column_name, $content, $order)

  • You can override the default filter function by using filter(function($query){})

  • You can use Blade Template Engine in your $content values

  • The name of columns is set by returned array.

    • That means, for 'posts.id' it is 'id' and also for 'owner.name as ownername' it is 'ownername'
  • You can easily toggle datatables mdata support by passing true/false on make function. ->make(true)

  • You can add DT_RowId via ->setRowId('id') function. Parameters could be like below:

    • a column on selected query if exists, else will just return the parameter
    • a closure function
    • a blade template
  • You can add DT_RowClass via ->setRowClass('id') function. Parameters could be like below:

    • a column on selected query if exists, else will just return the parameter
    • a closure function
    • a blade template
  • You can add DT_RowData via these functions:

    • ->setRowData(array()) to add batch data using an array
    • ->addRowData($key, $value) to append single data on array (Note: setRowData should be called first if you plan on using both functions)
    • the value parameter can also be a string, closure or blade template.
  • You can add DT_RowAttr via these functions:

    • Note: This option will only work on DataTables 1.10.5 or newer
    • ->setRowAttr(array()) to add batch data using an array
    • ->addRowAttr($key, $value) to append single data on array (Note: setRowAttr should be called first if you plan on using both functions)
    • the value parameters can also be a string, closure or blade template.
  • You can override default filter function on each column by using filterColumn function.

    • Usage: filterColumn($column, $method, $param_1, $param_2, ..., $param_n )
      • $column - the column name that search filter is be applied to

      • $method - can be any of QueryBuilder methods (where, whereIn, whereBetween, having etc.).

        Note: For global search, these methods are automaticaly converted to their "or" equivalents (if applicable, if not applicable, the column is not searched). If you do not want some column to be searchable in global search, set the javascript column flag searchable: false.

      • $param_1 ... $param_n - these are parameters that will be passed to the selected where function ($method). Possible types:

        • string
        • DB::raw() - The DB::raw() can output literaly everything into the query. For example, subqueries or branching if you need some really sophisticated wheres.
        • function - or any other callable
        • array of any of the above
  • Datatables now extends Query Builder's functionality which means that you can directy filter results using the Datatables class.

    // Query Builder's extended function.
    // See Laravel Query Builder docs for more details.
    return Datatables::of($user)
        ->whereIn('id', [1,2,3])
        ->whereNull('id')
        ->make(true);
  • You can use league\fractal to transform API data output by using setTransformer('App\Transformer\DataTransformer'). See Fractal Transformer docs for details.

  • [v5.2++] Datatables can now be use using IOC container like app('datatables')

  • [v5.2++] Datatables Engines can now be called along with IOC container. app('datatables')->usingQueryBuilder($builder)->make(true)

    • Available Engines:
      • Query Builder Engine. ->usingQueryBuilder($builder)->make()
      • Eloquent Engine. ->usingEloquent($model)->make()
      • Collection Engine. ->usingCollection($collection)->make()