In the daily dev requirement, we usually show data on the front end, and with Datatables it looks more interactive and smooth. In the same way, we are going to explore Yajra Datatables in the Laravel...
Gurpreet Kait
Author
In the daily dev requirement, we usually show data on the front end, and with Datatables it looks more interactive and smooth. In the same way, we are going to explore Yajra Datatables in the Laravel ecosystem and how we can use them.
Yajra Datatables is a package that makes it easy to create dynamic server-side data tables with ease. It can be an easy add-on for your next Laravel project.
If you are new to this concept don't worry, I'll explain everything that will help you to use this easily.
How To Install
In Laravel, this can be installed with the following command:
Practically, I will say that it has a lot of things that make it easier to use Datatables once you get comfortable with defining columns and all, you will see it makes life easier.
So, I'll list down all the concepts that I saw really provide value to the developer and not extra theoretical lines here.
It has the ability to modify columns by building Eloquent queries in it. Let me show you a small example of my code.
/**
* Get the query source of dataTable.
*/
public function query(ImportedAttendance $model): QueryBuilder
{
$request = $this->request();
// Log::info('request in payroll: '. json_encode($request->all()));
$designation = $request->designation ?? '';
$employeeCompany = $request->employee_company_id ?? '';
// $employeeId = $request->employee_id ?? '';
$department = $request->department ?? '';
$month = $request->month ?? '';
$year = $request->year ?? '';
// return$model->newQuery()->with();
$query = $model->newQuery();
$query->whereHas('employeeDetails', function ($query) use ($designation, $employeeCompany, $department) {
if ($designation !== 'all') {
$query->where('designation_id', $designation);
}
if ($employeeCompany !== 'all') {
$query->where('client_id', $employeeCompany);
}
// if ($employeeId !== 'all') {
// $query->where('employee_details.user_id', $employeeId);
// }
if ($department !== 'all') {
$query->where('department_id', $department);
}
});
$firstDayOfNextMonth = Carbon::createFromDate($year, $month, 1)->addMonth();
$lastDayOfMonth = $firstDayOfNextMonth->subDay();
$lastDayOfMonthString = $lastDayOfMonth->format('Y-m-d');
$query->where('month', $lastDayOfMonthString)->with();
return$query;
}
2. When we use query builder / Eloquent Models in it, It becomes more handy to show data on fronted instead of tackling all of these in the blade file.
3. it comes with an HTML builder, you can build the columns in it. In my point of view, it makes the blade much more clear because everything goes from the DataTable Class to our view file.
There are a lot of other things that you will understand when you use it.
Use Case Example - How to use Yajra Datatables
Now let's see how can you use this Datatable package practically in your Project.
So, the installation guide is already explained now. It provides an artisan command with which we can create a Datatable Class.
php artisan datatable:make Dummy #it will automatically add DataTable in the end of the name
Let's understand the above Datatable Class now. I have tried going a little deeper into the methods. So, I can explain but few things I don't know how to work under the hood. And also not important.
Method dataTable() returns the query instance with the result and performs an action with we can use each data in rawColumns.
There's a method called computed() which says that this column needs some extra modifications that we define in dataTable() method.
In query() method we can build queries for our uses, Maybe you want to use relations between models and joins so, you need to define in the query() method and then you can access that in your dataTable() method.
getColumns() You have Column::make('id'), a static method that makes () accept name of the field as in the table of the database.
If you want to you can add a title to the make('this_name_can_be_defined_in_datatable_method')->title(custom_title__for_the_column)->data(field_name_as_in_table).
compact() functions accept the string as a variable name. //output will be
If you want to send data to the view file, You can use the above way in render() method in the second parameter you can add whatever data you want.
render() method renders the view file with data that has been returned from dataTable.
View Implementation
In your view file, you can use it simply as mentioned below in the snippet.
{!! $dataTable->table() !!}
Conclusion
I hope that it will help you understand the basic use cases of Yajra Datatables. You will understand more once you start using this in your own projects. Thanks.