https://github.com/bitstudio-id/bitdatatable
A simple way to implement datatable with eloquent & query builder
https://github.com/bitstudio-id/bitdatatable
datatable eloquent javascript jquery-datatables laravel php query-builder
Last synced: 6 months ago
JSON representation
A simple way to implement datatable with eloquent & query builder
- Host: GitHub
- URL: https://github.com/bitstudio-id/bitdatatable
- Owner: bitstudio-id
- Created: 2019-07-12T08:05:58.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-07-15T00:50:15.000Z (about 1 year ago)
- Last Synced: 2025-11-21T08:30:57.144Z (8 months ago)
- Topics: datatable, eloquent, javascript, jquery-datatables, laravel, php, query-builder
- Language: PHP
- Homepage: https://packagist.org/packages/bitstudio-id/bitdatatable
- Size: 121 KB
- Stars: 6
- Watchers: 1
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# BITDataTable : jQuery DataTables for Laravel
This package is created to handle server-side works of DataTables jQuery Plugin via AJAX option by using Eloquent ORM / Query Builder.
## Quick Installation
```
composer require bitstudio-id/bitdatatable
```
## Requirements
- [PHP >= 5.6.4](http://php.net/)
- [Laravel >= 5.4](https://github.com/laravel/framework)
- [jQuery DataTables v1.10.x](http://datatables.net/)
## Javascript and CSS
```html
```
## Blade view
```html
#ID
Name
Role
Email
...
```
## Javascript
```
$(document).ready(function () {
//hide error/warning on datatable
$.fn.dataTable.ext.errMode = 'none';
var table = $('#table').DataTable({
//enable filter
bFilter: true,
processing: true,
serverSide: true,
ajax: {
url: "/dummy/dtb-v2/get",
type: 'get',
},
columns: [
{data: "id", name: "id", searchable: false, orderable: false},
{data: "employee.code"},
{data: "name", name: "user_name"},
{data: "employee.role.name"},
{data: "email"},
{data: "action", searchable: false, orderable: false}, // use searchable: false, orderable: false for custom column
],
});
});
```
## How to use with eloquent
```php
use BITStudio\BITDataTable\BITDataTable;
...
...
public function dtbGetV2(Request $request)
{
$dtb = new BITDataTable();
// Set request
$dtb->setRequest($request);
$user = User::query()->with('employee', 'employee.role');
$dtb->from($user);
$state = "admin";
$dtb->addCol(function ($user){
$user->action = "action-{$item->id}";
return $user;
});
return $dtb->generate();
}
```
## How to use logic on addCol
```
$state = "admin";
$dtb->addCol(function ($user) use ($state){
//use logic on addCol
//set as empty default
$user->admin_col = "";
if($state == $user->role->name) {
$user->admin_col .= "admin-col";
}
return $user;
});
```
## How to use with Query Builder
```php
use BITStudio\BITDataTable\BITDataTable;
...
...
public function dtbGetV2(Request $request)
$dtb = new BITDataTable();
$dtb->setRequest($request);
$q = DB::table("orders as o");
$q->select("o.*", "o.no_cs as customer_number", "e.employee_name as emp_name");
$q->leftJoin("employee as e", "e.id", "=", "o.employee_id");
$dtb->from($q);
//add custom column
$dtb->addCol(function ($user){
$user->action = "action-{$item->id}";
return $user;
});
return $dtb->generate();
}
```
### How to show index number for numbering on view
```
$dtb->setRowIndex(true);
```
this will append property DT_RowIndex on json response
```
columns: [
{data: "DT_RowIndex", name: "id" searchable: false, orderable: false},
...
]
```
dont forget to set searchable = false
#### add value class attribute
```
$dtb->addClass("text-danger"); //insert before genereate
```
#### add value id attribute
```
//create custom from collection property
$dtb->setRowId("id");
//create custom from addCol or setRowId for custom id attribute
$dtb->setRowId(function($item) {
$item->DT_RowId = "id-".$item->id;
return $item;
});
```
## License
The MIT License (MIT). Please see License File for more information.