1. Go to this page and download the library: Download bored-programmers/laragrid 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/ */
bored-programmers / laragrid example snippets
use BoredProgrammers\LaraGrid\Components\ColumnComponents\Column;
use BoredProgrammers\LaraGrid\Livewire\BaseLaraGrid;
use Illuminate\Database\Eloquent\Builder;
use BoredProgrammers\LaraGrid\Filters\FilterResetButton;
class MyGrid extends BaseLaraGrid
{
protected function getColumns(): array
{
return [
Column::make('id', 'ID'),
Column::make('name', 'Name'),
// Add more columns as needed
];
}
protected function getDataSource(): Builder
{
return MyModel::query();
}
}
use BoredProgrammers\LaraGrid\Theme\BaseLaraGridTheme;
use BoredProgrammers\LaraGrid\Theme\FilterTheme;
use BoredProgrammers\LaraGrid\Theme\TBodyTheme;
use BoredProgrammers\LaraGrid\Theme\THeadTheme;
class MyTheme extends BaseLaraGridTheme
{
public static function make(): static
{
$theme = new static();
$theme->setTableClass('min-w-full table-auto');
$theme->setTheadTheme(
THeadTheme::make()
->setTheadClass('pb-4')
->setThClass('pb-3 px-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap')
);
$theme->setTbodyTheme(
TBodyTheme::make()
->setEmptyMessageClass('text-white')
->setTdClass('whitespace-nowrap p-3 text-sm text-gray-500')
->setGroupTdClass('whitespace-nowrap flex items-center p-3 text-sm text-gray-500')
->setRecordTrClass(fn($record) => $record->role === 'admin' ? 'bg-red-500' : 'bg-white'); // you can also set a closure for record tr class. Pass a closure that returns a string class.
->setRecordTrClass('bg-white odd:bg-gray-100'); // If you don't want to set a closure, you can just pass a string class.
);
$theme->setFilterTheme(
FilterTheme::make()
->setFilterTextClass('bg-white w-full rounded-xl border border-gray-300')
->setFilterSelectClass('bg-white w-full rounded-xl border border-gray-300')
->setFilterDateClass('bg-white w-full rounded-xl border border-gray-300')
);
// those are not all methods, you can find all of them in BaseLaraGridTheme, THeadTheme, TBodyTheme and FilterTheme classes
return $theme;
}
}
protected function getTheme(): BaseLaraGridTheme
{
return MyTheme::make();
}
abstract class MyBaseGrid extends BaseLaraGrid
{
#[Url]
public array $filter = [];
#[Url(except: 'id')]
public string $sortColumn = 'id';
#[Url]
public string $sortDirection = 'desc';
protected function getFilterResetButton(): FilterResetButton
{
return FilterResetButton::make();
}
protected function getTheme(): BaseLaraGridTheme
{
return MyTheme::make();
}
}