Use the for()
method to define the model or the table name to get data from.
this method accepts Model::query()
or srting
datatableCruds()->for('users');
datatableCruds()->for(User::class);
datatableCruds()->for(User::query());
when you need to call column or input from table relation you must use this method to get the relation data.
this method accepts list of string
arguments.
$datatable->with('relation1', 'relation2:column1,column2', 'relation1.relation2', ...);
use it to set a custom title for the page.
$datatable->setPageTitle("DataTables");
use it to set a custom direction for the page default is "ltr"
.
$datatable->setDir("rtl");
use it to set a custom header with all requests, you can call it for every header you need to set. the first argument is for header name and the second one is for the header value. you can write javascript code in the second argument
$datatable->setHeader('X-CUSTOM-HEADER', '`Bearer ${localStorage.getItem("token")}`');
use it to set the columns to search by. or you can just use searchable()
method while rendering the column, we will explain it recently.
this method accepts list of string
arguments.
$datatable->searchBy('id', 'name->en', ...);
Select Filter will filter table data with selected option value
$datatable->selectFilter(
filterBy: "profile.gender",
options: [
"male" => "Male",
"female" => "Female"
],
label: "Gender",
// defaultValue: "male" || null
);
// you can filter with whereNotNull by make value ='!null' like below example
$datatable->selectFilter(
"deleted_at",
[
"null" => "Not Trashed",
"!null" => "Trashed"
],
"Users Status",
);
you can use this method to remove export print button or to set your own button.
$datatable->printBtn(false);
use it to set columns default date format. by default it's human format.
$datatable->setDefaultDateFormat("YYYY-MM-DD");
use it to set the default get data order.
by default its ("created_at", "desc")
.
$datatable->setDefaultOrder("created_at", "desc");
all of these methods works in the same way.
if you want to disable any button of these buttons just pass false to method
use it to set custom add button.
the first argument of the method is button html,
the second one is bool isJS if true will execute JS code to it,
the third one is button action you can set it to one of these choices ("openModal", "funcName", "href")
default is "openModal"
and the fourth argument is for action value if you set the button action to "href"
you will need to pass href value in the third argument
and if you set the button action to "funcName"
you will need to pass a javascript function name to execute onClick on the add button.
by default add button will open modal for form store.
if you send funcName
, when you create this function in your app you will get two arguments in it (event, row)
row which will contain the row data but in method rowAddButton()
you will only get the event (event)
.
$datatable->rowAddButton('<button class="btn btn-primary"> Add New User </button>');
// in the below exapmle the rowDeleteButton will not be render in actions column unless deleted_at is null
// you must make isJS true to able to execute JS code.
$datatable->rowDeleteButton(
html: <<<JS
'|deleted_at|' ? false : "<i class='fa fa-trash'></i>"
JS,
isJS: true
)
use it to set the search debounce time.
the first argument of the method is the debounce time and the second one is for the class name of the search input, by default it is "form-control"
.
$datatable->search('500ms');
use it to update any text.
the first argument is for the text key and the second one is for its value.
you will find all available text keys at config/datatablecruds.php
$datatable->setText("delete.title", "Delete");
$datatable->setText("info", "Showing |from| to |to| of |total| entries");
use it to set select limit entries options.
this method accepts list of int
arguments.
$datatable->setLimits(10, 20, 30, 40, ...);
use it to set custom width to form.
$datatable->formWidth("40%");
use it to set custom height to form.
$datatable->formHeight("200px");
use it to set custom label and color to form store button.
by default the label is "Create"
and the color is "primary"
$datatable->formStoreButton("Create", "primary");
use it to set custom label and color to form update button.
by default the label is "Update"
and the color is "primary"
$datatable->formUpdateButton("Update", "primary");
use it to set custom label and color to form delete button.
by default the label is "Delete"
and the color is "danger"
$datatable->formDeleteButton("Delete", "danger");
this method accepts one boolean prameter, by default it's true.
$datatable->showPagination(false);
this method accepts one boolean prameter, by default it's true.
$datatable->hidePaginationIfContainOnePage(false);
this method will return an array and you can use it with @datatable
directive to render datatable cruds.
if you want to render one datatable only it's better for you to use render method instead of this
$datatable = $datatable->renderData();
return view('app', compact('datatable'));
// in app.blade.php file render this directive
@datatable($datatable)
use this method to render your datatable view.
this method accepts one prameter which is a string of the view name to render in or an array of variables that can be accessed in the @extends
(layout) blade file, and in this case the datatable will be renderd in the layout blade file.
$datatable->render('users');
OR
$datatable->render(["title" => "datatable"]);
use this method to determine whether the current table request is an XHR (XMLHttpRequest) or not.
$datatable->isXhr();
use this method to obtain a LengthAwarePaginator result.
$datatable->getResults();
use this method in Blade to render the table UI.
$datatable->table();
use this method to set extends
directive to add datatable in your blade layout.
you can set the default extends
from datatablecruds config file
$datatable->setBladeExtendsName("app");
use this method to set section
directive name that will go to yield
directive in the blade layout.
you can set the default section
from datatablecruds config file
$datatable->setBladeSectionName("content");
use this method to push section to blade file.
$datatable->pushSectionToBlade("title", "value");
use this method to push stack to blade file.
$datatable->pushStackToBlade("title", "value");