PHPackages                             apphp/laravel-datagrid - 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. apphp/laravel-datagrid

ActiveLibrary[Framework](/categories/framework)

apphp/laravel-datagrid
======================

DataGrid helpers for easy creating CRUD in Laravel Framework Applications

1.1.0(2y ago)9133MITPHPPHP &gt;=7.1

Since Dec 21Pushed 2y ago2 watchersCompare

[ Source](https://github.com/apphp/laravel-datagrid)[ Packagist](https://packagist.org/packages/apphp/laravel-datagrid)[ Docs](https://apphp.com)[ RSS](/packages/apphp-laravel-datagrid/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (10)Dependencies (3)Versions (15)Used By (0)

[![License: MIT](https://camo.githubusercontent.com/1a2e0606685ce00663bf829868f794fd3fc9c86f8d80cae324734129e0723a58/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d627269676874677265656e2e737667)](https://opensource.org/licenses/MIT)

DataGrid helpers for Laravel Framework Applications
===================================================

[](#datagrid-helpers-for-laravel-framework-applications)

This package helps to create DataGrid (CRUD) pages for Laravel 6+ framework applications.

Requirements
------------

[](#requirements)

- PHP &gt;=7.1
- Laravel 6+
- Bootstrap 3+

License
-------

[](#license)

This project is released under the MIT License.
Copyright © 2020 [ApPHP](https://www.apphp.com/).

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

[](#installation)

Begin by pulling in the package through Composer.

```
composer require apphp/laravel-datagrid
```

Next, make sure you connected Bootstrap. You may either pull in the Bootstrap's CSS within your HTML or layout file, or write your own CSS classes based on them.

```

```

If you need to modify the datagrid files, you can run:

```
php artisan vendor:publish --provider="Apphp\DataGrid\DataGridServiceProvider"
```

Usage in Controllers
--------------------

[](#usage-in-controllers)

### 1. Import classes

[](#1-import-classes)

```
use Apphp\DataGrid\Pagination;
use Apphp\DataGrid\Filter;
```

### 2. Define filters and filter field types

[](#2-define-filters-and-filter-field-types)

```
$filters      = [
    'act' => ['type' => 'equals', 'value' => 'search'],
    'email'    => ['title' => 'Email', 'type' => 'string', 'compareType' => '%like%', 'validation' => ['maxLength' => 150]],
    'name'     => ['title' => 'Name', 'type' => 'string', 'compareType' => '%like%'],
    'username' => ['title' => 'Username', 'type' => 'string', 'compareType' => '%like%'],
    'user_id'  => ['title' => 'ID', 'type' => 'integer', 'compareType' => '=', 'validation' => ['max' => 10000000]],
];
```

Following filter field types are available

TypeDescription`string`Any type of strings`integer` or `int`Numeric integer field (HTML type="number" attribute is used)`set`Set of values (array)`date`The datetime fieldsEach filter field can include following attributes:

AttributeTypeDescription`title`StringSpecifies a title, that will be shown in the label of filter field`type`StringSpecifies a type of the filter field (see above)`compareType`StringSpecifies which type of comparison will be used: ex.: '=', '%like%', '!=' etc.`source`ArraySpecifies the source (array) to 'set' fields`validation`ArraySpecifies validation rules (array). Possible options: \['minLength'=&gt;2, 'maxLength'=&gt;10, 'min'=&gt;2, 'max'=&gt;100\]`relation`StringSpecifies the relation between 2 models (One-to-One, One-to-Many), ex.: search in posts for users - relation="posts"`relationXref`StringSpecifies the relation between 2 models (Many-to-Many), ex.: search in roles for users - relation="roles"`htmlOptions`ArraySpecifies any possible HTML attribute for the field`disabled`BooleanSpecifies whether the field is disabled or not (default - not)### 3. Handle filters and prepare SQL builder

[](#3-handle-filters-and-prepare-sql-builder)

```
// $query = User::sortable()->orderByDesc('id');
$query = User::orderByDesc('id');
$request = request(); // or get it via function param, like foo(Request $request){...}
$url = route('backend.users.submitRote');
$cancelUrl = $url;
$filters = [];

$filter       = Filter::init($query, $request, $filters, $url, $cancelUrl, 'collapsed');
$filter       = $filter::filter();
$filterFields = $filter::getFilterFields();
$query        = $filter::getQuery();
```

### 4. Sorting

[](#4-sorting)

```
$sort      = $request->get('sort');
$direction = $request->get('direction');
```

### 5. Pagination

[](#5-pagination)

```
$pagination       = Pagination::init($query, 20, $sort, $direction, $filterFields)::paginate();
$paginationFields = $pagination::getPaginationFields();
$users            = $pagination::getRecords();
```

### 6. Rendering view

[](#6-rendering-view)

```
return view('backend.users.mainView', compact('users', 'filterFields', 'paginationFields'));
```

Usage in View files
-------------------

[](#usage-in-view-files)

```

    {!! \Apphp\DataGrid\Filter::renderJs() !!}

@if(count($records))
    {!! \Apphp\DataGrid\Filter::renderFields() !!}

        @foreach ($records as $record)

        @endforeach

    {!! \Apphp\DataGrid\Pagination::renderLinks() !!}
@else
    {!! \Apphp\DataGrid\Message::warning('Sorry, no records were found. Please adjust your search criteria and try again.') !!}
@endif
```

Configuration
-------------

[](#configuration)

To change default settings and enable some extra features you can export the config file:

```
php artisan vendor:publish --tag=laravel-datagrid:config
```

Localization
------------

[](#localization)

To change or add new translation files you can export the language files:

```
php artisan vendor:publish --tag=laravel-datagrid:lang
```

Customize Views
---------------

[](#customize-views)

To change HTML template of the datagrid or use your own, publish view file and customize it to suit your needs.

```
$ php artisan vendor:publish --tag=laravel-datagrid:views
```

Now you should have a datagrid.php file in the config folder of your application. If you need to force to re-publish the config file to use `--force`.

Testing
-------

[](#testing)

To rum unit testing simply do following:

```
./vendor/bin/phpunit vendor\\apphp\\laravel-datagrid\\tests\\TestDataGridMessage.php
```

or your may add additional section to your composer.json file:

```
"scripts": {
    "tests": "phpunit --colors=always",
    "test": "phpunit --colors=always --filter",
}
```

and then rum unit following command:

```
composer tests vendor\\apphp\\laravel-datagrid\\tests\\TestDataGridMessage.php
composer tests vendor\\apphp\\laravel-datagrid\\tests\\TestDataGridPagination.php
composer tests vendor\\apphp\\laravel-datagrid\\tests\\TestDataGridFilter.php
```

and so on...

Examples
--------

[](#examples)

### Controller code (full example)

[](#controller-code-full-example)

```
public function index(Request $request)
{
    // Additional data
    $roles    = Role::rolesList();
    $statuses = User::statusesList();
    $actives  = [0 => 'Not Active', 1 => 'Active'];

    // Define filters and filter field types
    $filters      = [
        'act' => ['type' => 'equals', 'value' => 'search'],

        'email'    => ['title' => 'Email', 'type' => 'string', 'compareType' => '%like%', 'validation' => ['maxLength' => 150]],
        'name'     => ['title' => 'Name', 'type' => 'string', 'compareType' => '%like%'],
        'username' => ['title' => 'Username', 'type' => 'string', 'compareType' => '%like%'],
        'user_id'  => ['title' => 'ID', 'type' => 'integer', 'compareType' => '=', 'validation' => ['max' => 10000000]],

        'role'       => ['title' => 'Role', 'type' => 'user_role', 'compareType' => '', 'source' => $roles],
        'status'     => ['title' => 'Status', 'type' => 'user_status', 'compareType' => '', 'source' => $statuses],
        'active'     => ['title' => 'Active', 'type' => 'user_active', 'compareType' => '', 'source' => $actives],

        'created_at'    => ['title' => 'Created At', 'type' => 'date', 'compareType' => 'like%'],
        'last_logged_at' => ['title' => 'Last Login', 'type' => 'date', 'compareType' => 'like%'],
    ];

    $query = User::orderByDesc('id');

    // Handle filters and prepare SQL query
    $filter       = Filter::init($query, $request, $filters, route('users.list'), route('users.list'), 'collapsed');
    $filter       = $filter::filter();
    $filterFields = $filter::getFilterFields();
    $query        = $filter::getQuery();

    // Sorting
    $sort      = $request->get('sort');
    $direction = $request->get('direction');

    // Pagination
    $pagination       = Pagination::init($query, 20, $sort, $direction, $filterFields)::paginate();
    $paginationFields = $pagination::getPaginationFields();
    $users            = $pagination::getRecords();

    return view('users.list', compact('users', 'filterFields', 'paginationFields'));
}
```

### Sorting

[](#sorting)

If you use some kind of packages for column sorting, like `kyslik/column-sortable`, you have to change usage of Model to following: Without sorting

```
$query = User::orderByDesc('id');
```

With column sorting

```
$query = User::sortable()->orderByDesc('id');
```

### Table content rendering

[](#table-content-rendering)

You have 2 way to render table content. The first is to write creating table manually in view file. Look on example below:

```

        @sortablelink('user_id', 'ID')
        ...

        @foreach ($users as $user)

                {{ $user->user_id }}
                ...

        @endforeach

```

The second way is to use `GridView` helper. Look on example below:

```
// GridView - initialized in Controller
$gridView = GridView::init($records);

return view('backend.users', compact(..., 'gridView'));
```

```
{{-- Render table content --}}
{!!
    $gridView::renderTable([
        'user_id'           => ['title' => 'ID', 'width'=>'60px', 'headClass'=>'text-right', 'class'=>'text-right', 'sortable'=>true, 'callback'=>null],
        'username'          => ['title' => 'Username', 'width'=>'', 'headClass'=>'text-left', 'class'=>'', 'sortable'=>true],
        'name'              => ['title' => 'Name', 'width'=>'', 'headClass'=>'text-left', 'class'=>'', 'sortable'=>true],
        'email'             => ['title' => 'Email', 'width'=>'', 'headClass'=>'text-left', 'class'=>'text-truncate px-2', 'sortable'=>true],
        'created_at'        => ['title' => 'Created At', 'width'=>'160px', 'headClass'=>'text-center', 'class'=>'text-center px-1', 'sortable'=>true],
        'last_login_at'     => ['title' => 'Last Login', 'width'=>'160px', 'headClass'=>'text-center', 'class'=>'text-center px-1', 'sortable'=>false],
    ])
!!}
```

You may also use a `callback` attribute to customize values of the specific field. This attribute accepts a function, link to function or a closure. Below you may find few examples to get a feel:

Show specific badge if user has verified

```
'callback'=>function($user){ return $user->isVerified() ? 'Verified' : 'Waiting'; }
```

Show a list of user roles, get array of roles via `$roles` parameter

```
'callback'=>function($user) use ($roles){ $output = ''; if(!count($user->roles)) $output .= 'User'; foreach($user->roles as $role) { $output .= ''.$roles[$role->name].' '; } return $output; }
```

Show user's avatar with a link to edit

```
'callback'=>function($user){ return ' '.$user->username.''; }
```

### Collapsed filter

[](#collapsed-filter)

[![Collapsed filter](https://raw.githubusercontent.com/apphp/laravel-datagrid/master/images/filter-collapsed.png)](https://raw.githubusercontent.com/apphp/laravel-datagrid/master/images/filter-collapsed.png)

### Expanded filter

[](#expanded-filter)

[![Expanded filter](https://raw.githubusercontent.com/apphp/laravel-datagrid/master/images/filter-expanded.png)](https://raw.githubusercontent.com/apphp/laravel-datagrid/master/images/filter-expanded.png)

### Pagination

[](#pagination)

[![Pagination](https://raw.githubusercontent.com/apphp/laravel-datagrid/master/images/paganation.png)](https://raw.githubusercontent.com/apphp/laravel-datagrid/master/images/paganation.png)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~73 days

Recently: every ~234 days

Total

14

Last Release

1020d ago

Major Versions

0.7.0 → 1.0.02021-01-23

PHP version history (2 changes)0.1.0PHP &gt;=7.0.0

0.4.0PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/9803e512c3636b0cfa0224e247621e178ad0fe34f399a413be9b63148f636b70?d=identicon)[apphp](/maintainers/apphp)

---

Top Contributors

[![apphp](https://avatars.githubusercontent.com/u/5477692?v=4)](https://github.com/apphp "apphp (80 commits)")

---

Tags

apphpdatagrid-helpersfilter-helperslaravellaravel-crudlaravel-packagelaravel6laravel6-packagepagination-helperslaravellaravel-packagelaravel6laravel crudlaravel6-packageapphpdatagrid helpersfilter helperspagination helpers

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/apphp-laravel-datagrid/health.svg)

```
[![Health](https://phpackages.com/badges/apphp-laravel-datagrid/health.svg)](https://phpackages.com/packages/apphp-laravel-datagrid)
```

###  Alternatives

[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k84.2M225](/packages/laravel-horizon)[laravel/ui

Laravel UI utilities and presets.

2.7k134.9M601](/packages/laravel-ui)[laravel/jetstream

Tailwind scaffolding for the Laravel framework.

4.1k19.8M136](/packages/laravel-jetstream)[internachi/modular

Modularize your Laravel apps

1.1k662.4k8](/packages/internachi-modular)[imanghafoori/laravel-smart-facades

Adds some features on the top of laravel facades

137415.3k7](/packages/imanghafoori-laravel-smart-facades)[binafy/laravel-stub

Generate stub files easy

99158.8k10](/packages/binafy-laravel-stub)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
