PHPackages                             mzprog/datatables - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. mzprog/datatables

ActiveLibrary[Framework](/categories/framework)

mzprog/datatables
=================

simple laravel/livewire datatable easy to use

v0.1(3y ago)08MITPHP

Since Dec 3Pushed 3y ago1 watchersCompare

[ Source](https://github.com/mzprog/datatables)[ Packagist](https://packagist.org/packages/mzprog/datatables)[ RSS](/packages/mzprog-datatables/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

datatables
==========

[](#datatables)

create a better tables with few lines of code.

Installation
------------

[](#installation)

simply install the package by:

```
composer require mzprog/datatables

```

make sure that livewire is installed, and styles and scripts added to the blade layout: `@livewireStyles` and `@livewireScripts`

Basic Usage
-----------

[](#basic-usage)

- create a livewire component, without view file, and without `render` method.
- The component should extends `Mzprog\Datatables\Datatable`
- add `columns` method to your component
    - `public function columns(): array;`
    - This Should return array of `Mzprog\Datatables\Column`
- add `query` method to have the main query
    - `public function query() : Builder`

### Column class

[](#column-class)

just add `Column::name('id')` to your columns array, and it will be added and viewed in your table, but it will be labeled as `Id`.

To change the label name just use the second parameter `Column::name('id', 'ID')`.

The name will be used as the array index, and database field by defualt.

`field` method is used to allow you to different key for the column as in this example(`orderable`&amp; `edit` will be explained later):

```
Column::name('added')->field('created_at')
    ->orderable()
    ->edit(fn($row) => $row->created_at->diffForHumans()),

```

we have change the format for created\_at but can't store it in same properties (because casted to date, and work as `getCreateAtAttribute`).

so when use `orderable` it will order using `created_at`.

`edit` method is used to change or add data.
it accept callback with the current raw data as parameter, example:

```
Column::name('full_name', 'Name)
    ->edit(fn($raw) => "{$raw->first_name} {$raw->last_name}")

```

`orderable`method will allow you to order the column by pressing on the column name. it will order your data based on your field name, unless you add a callback as a parameter. the callback parameter are: the query `Builder`, and the order direction, example:

```
return [
    Column::name('id', 'ID')->orderable(),
    Column::name('full_name', 'Name)
        ->orderable(fn($q, $dir) => $q->orderBy('first_name', $dir)->orderBy('last_name', $dir))
        ->edit(fn($raw) => "{$raw->first_name} {$raw->last_name}"),
];

```

`searchable` method is work as orderable, and the callback function will pass the search keyword instead of order direction, example:

```
Column::name('full_name', 'Name)
    ->searchable(
        fn($q, $keyword) => $q
            ->where(DB::raw('CONCAT(first_name," ", last_name)'), 'like', "%${keyword}%")
    )
    ->orderable(fn($q, $dir) => $q->orderBy('first_name', $dir)->orderBy('last_name', $dir))
    ->edit(fn($raw) => "{$raw->first_name} {$raw->last_name}"),

```

`raw` method is used when you need to print HTML in your table. example:

```
Column::name('actions')->raw()
    ->edit(fn($row) => 'View';

```

### Filter Class

[](#filter-class)

If you want to select one or more value as a filter, like you only need to see rows from today, you can use:

```
public function filters()
{
	return [
        Filter::name('date', 'Select a Date'),
    ];
}

```

this will let you filter from your table using `date` column, also you can skip the label name, by defualt it will be `Date`.

also you can use custom data and filters:

example 1 (if you want to filter by name first letter):

```
Filter::name('name')->options(function () {
    $options = User::query()
        ->select([
            DB::raw('LEFT(name,1) as letter'),
            DB::raw('COUNT(*) as total')
        ])->groupBy('letter')->get();

    return $options->map(fn ($d) => [
        'value' => $d['letter'],
        'name' => "Starts with '{$d['letter']}'",
        'total' => $d['total'],
    ])->toArray();
})
->filter(function (Builder $query, array $values) {
    $query->whereIn(DB::raw('LEFT(name,1)'), $values);
})

```

for `options` you need to return array of options, and option has (name, value, total).

`filter` if this filter is selected you can add conditions to the provided query, and you will get also array of the selected values.

example 2(filter by success: success, fail)

```
Filter::name('success')->options(function () {
    return [
        [
            'name' => 'Success',
            'value' => '>=',
            'total' => Exam::where('points', ">=", 50)->count()
        ],
        [
            'name' => 'Fail',
            'value' => '
