churakovmike/laravel-grid

Easy gridview for laravel

1.1 2020-03-21 17:09 UTC

This package is auto-updated.

Last update: 2022-09-10 22:36:43 UTC


README

Latest Stable Version Build Status License Maintainability Test Coverage Scrutinizer Code Quality

Requirements

  • laravel 5.5+
  • Bootstrap 3/4 for styling

Getting started

install

The package is available on packagist.

composer require churakovmike/laravel-grid

Register service provider in config/app.php

ChurakovMike\EasyGrid\GridViewServiceProvider::class,       

Grid example

<?php

namespace App\Http\Controllers;

use ChurakovMike\EasyGrid\DataProviders\EloquentDataProvider;

class ExampleController extends Controller
{
    public function example()
    {
        $dataProvider = new EloquentDataProvider(ExampleModel::query());
        return view('example-view', [
            'dataProvider' => $dataProvider,
        ]);
    }
}

Render grid simple example

{!! easy_grid([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        'name',
        'email',
        'status',
        'created_at',
    ],
]) !!}

Render grid custom field and callbacks

{!! easy_grid([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        [
            'label' => 'Users name',
            'attribute' => 'name',
        ],
        [
            'label' => 'Custom label',
            'attribute' => 'id'
        ],
        [
            'label' => 'Example callbacks',
            'value' => function($data) {
                return $data->relatedModel->attribute;
            }
        ],
        'email',
        'status',
        'created_at',
    ],
]) !!}

If you need column without model attribute, you can use callback

{!! easy_grid([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'label' => 'There are no model attribute',
            'value' => function($data) {
                return 'example string';
            }
        ],
        'email',
        'status',
        'created_at',
    ],
]) !!}

The grid support column fomatter, default is a text filter and it cuts out all html code (strip_tags()), you can change this formatter to html formatter

{!! easy_grid([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'label' => 'Avatar',
            'attribute' => 'avatar',
            'format' => 'html,
        ],
    ],
]) !!}

There are default action buttons in the grid,

{!! easy_grid([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        'name',
        'email',
        'status',
        'created_at',
        [
            'class' => \ChurakovMike\EasyGrid\Columns\ActionColumn::class,
            'buttons' => [
                'show',
                'update',
                'destroy',
            ],
        ],
    ],
]) !!}

Buttons urls

Button Url
Show /{id}
Update /{id}/edit
Destroy /{id}/delete

If you need another urls, just do like this

{!! easy_grid([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        'name',
        'email',
        'status',
        'created_at',
        [
            'class' => \ChurakovMike\EasyGrid\Columns\ActionColumn::class,
            'buttons' => [
                'show' => function($data) {
                    return route('your-route-name', [
                        'id' => $data->id,
                    ])
                },
                'update' => function($data) {
                    return '/edit/' . $data->id,
                },
                'destroy',
            ],
        ],
    ],
]) !!}

To change the width of the cell you can pass the value of the width

{!! easy_grid([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        [
            'label' => 'Users name',
            'attribute' => 'name',
            'width' => '15%',
        ],
        [
            'label' => 'Custom label',
            'attribute' => 'id',
            'width' => '100px',
        ],
        'email',
        'status',
        'created_at',
    ],
]) !!}