PHP code example of nabeeljavaid / laravel-grid
1. Go to this page and download the library: Download nabeeljavaid/laravel-grid library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nabeeljavaid / laravel-grid example snippets
'aliases' => [
// ...
'Grid' => NabeelJavaid\LaravelGrid\LaravelGrid::class,
],
use NabeelJavaid\LaravelGrid\LaravelGrid;
use App\Models\User;
public function index(LaravelGrid $grid)
{
$grid->setGridName('users-grid')
->setBaseUrl(route('users.index'))
->setPrimaryKey('id')
->setSerialNumber(true)
->setColumns([
[
'name' => 'name',
'label' => 'Name',
'width' => '25%',
'sortable' => true,
'searchable' => true,
'value' => fn($row) => e($row->name),
],
[
'name' => 'email',
'label' => 'Email',
'width' => '25%',
'sortable' => true,
'searchable' => true,
'searchfield' => ['type' => 'text'],
'value' => fn($row) => e($row->email),
],
[
'name' => 'status',
'label' => 'Status',
'width' => '15%',
'sortable' => false,
'searchable' => true,
'searchfield' => [
'type' => 'select',
'options' => ['active' => 'Active', 'inactive' => 'Inactive'],
],
'value' => fn($row) => $row->status === 'active'
? '<span class="badge bg-success">Active</span>'
: '<span class="badge bg-secondary">Inactive</span>',
],
[
'name' => 'actions',
'label' => 'Actions',
'width' => '10%',
'sortable' => false,
'searchable' => false,
'value' => fn($row) => '<a href="' . route('users.edit', $row->id) . '" class="btn btn-sm btn-success"><i class="fa fa-edit"></i></a>',
],
])
->setMainActions([
['name' => 'refresh', 'title' => 'Refresh', 'url' => route('users.index')],
['name' => 'add', 'title' => 'Add User', 'url' => route('users.create')],
['name' => 'export', 'title' => 'Export', 'url' => route('users.export')],
])
->setBulkActions([
route('users.bulk-delete') => 'Delete Selected',
])
->setPaginator(User::query(), 'name', 'asc');
return view('users.index', compact('grid'));
}
bash
# Config file -> config/laravel-grid.php
php artisan vendor:publish --tag=laravel-grid-config
# CSS/JS -> public/vendor/laravel-grid
php artisan vendor:publish --tag=laravel-grid-assets
# Blade views -> resources/views/vendor/laravel-grid
php artisan vendor:publish --tag=laravel-grid-views