General Methods


01. For Method

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());

02. with Method

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', ...);

03. setPageTitle Method

use it to set a custom title for the page.

$datatable->setPageTitle("DataTables");

04. setDir Method

use it to set a custom direction for the page default is "ltr".

$datatable->setDir("rtl");

05. setHeader Method

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")}`');

06. searchBy Method

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', ...);

07. selectFilter Method

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",
);

08. printBtn Method

you can use this method to remove export print button or to set your own button.

$datatable->printBtn(false);

09. setDefaultDateFormat Method

use it to set columns default date format. by default it's human format.

$datatable->setDefaultDateFormat("YYYY-MM-DD");

10. setDefaultOrder Method

use it to set the default get data order. by default its ("created_at", "desc").

$datatable->setDefaultOrder("created_at", "desc");

11. rowAddButton, rowEditButton, rowDeleteButton and rowCloneButton Methods

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
)

12. search Method

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');

13. setText Method

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");

14. setLimits Method

use it to set select limit entries options. this method accepts list of int arguments.

$datatable->setLimits(10, 20, 30, 40, ...);

15. formWidth Method

use it to set custom width to form.

$datatable->formWidth("40%");

16. formHeight Method

use it to set custom height to form.

$datatable->formHeight("200px");

17. formStoreButton Method

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");

18. formUpdateButton Method

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");

19. formDeleteButton Method

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");

20. showPagination Method

this method accepts one boolean prameter, by default it's true.

$datatable->showPagination(false);

21. hidePaginationIfContainOnePage Method

this method accepts one boolean prameter, by default it's true.

$datatable->hidePaginationIfContainOnePage(false);

22. renderData Method

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)

23. render Method

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"]);

24. isXhr Method

use this method to determine whether the current table request is an XHR (XMLHttpRequest) or not.

$datatable->isXhr();

25. getResults Method

use this method to obtain a LengthAwarePaginator result.

$datatable->getResults();

26. table Method

use this method in Blade to render the table UI.

$datatable->table();

27. setBladeExtendsName Method

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");

28. setBladeSectionName Method

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");

29. pushSectionToBlade Method

use this method to push section to blade file.

$datatable->pushSectionToBlade("title", "value");

30. pushStackToBlade Method

use this method to push stack to blade file.

$datatable->pushStackToBlade("title", "value");