Skip to content

Instantly share code, notes, and snippets.

@lotyp
Created March 25, 2024 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotyp/71b4f959cab901f829d9f98a487174c9 to your computer and use it in GitHub Desktop.
Save lotyp/71b4f959cab901f829d9f98a487174c9 to your computer and use it in GitHub Desktop.
Using spiral/data-grid in Laravel.
<?php
declare(strict_types=1);
namespace WayOfDev\Tests;
use Database\Factories\PostFactory;
use Illuminate\Support\Facades\Artisan;
use PHPUnit\Framework\Attributes\Test;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Spiral\DataGrid\GridFactory;
use Spiral\DataGrid\GridInterface;
use Spiral\DataGrid\Input\ArrayInput;
use WayOfDev\App\Entities\Post;
use WayOfDev\App\PostRepository;
use WayOfDev\App\PostSchema;
use function iterator_to_array;
class DataGridFactoryTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
Artisan::call('cycle:migrate:init');
Artisan::call('cycle:orm:render', ['--no-color' => true]);
Artisan::call('cycle:orm:migrate', ['--split' => true, '--run' => true]);
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getPost(): Post
{
$factory = PostFactory::new()->times(30)->create();
/** @var Post $post */
return $factory->random();
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
#[Test]
public function it_queries_data(): void
{
$post = $this->getPost();
$schema = new PostSchema();
$factory = $this->app->make(GridFactory::class);
$factory = $factory->withInput(new ArrayInput([
'title' => $post->title(),
]))->withDefaults([
GridFactory::KEY_FILTER => ['title' => $post->title()],
]);
$repository = $this->app->make(PostRepository::class);
/** @var GridInterface $result */
$grid = $factory->create($repository->select(), $schema);
$values = [];
foreach ([
GridInterface::FILTERS,
GridInterface::SORTERS,
GridInterface::COUNT,
GridInterface::PAGINATOR,
] as $key) {
$values[$key] = $grid->getOption($key);
}
dd([
'posts' => iterator_to_array($grid),
'grid' => [
'values' => $values,
],
]);
}
}
<?php
declare(strict_types=1);
namespace Database\Factories;
use WayOfDev\App\Entities\Post;
use WayOfDev\DatabaseSeeder\Factories\AbstractFactory;
class PostFactory extends AbstractFactory
{
public function makeEntity(array $definition): object
{
return new Post(
title: $definition['title'],
content: $definition['content'],
);
}
public function entity(): string
{
return Post::class;
}
public function definition(): array
{
return [
'title' => $this->faker->sentence,
'content' => $this->faker->paragraph,
];
}
}
<?php
declare(strict_types=1);
namespace WayOfDev\App;
use Spiral\DataGrid\GridSchema;
use Spiral\DataGrid\Specification\Filter\Equals;
use Spiral\DataGrid\Specification\Pagination\PagePaginator;
use Spiral\DataGrid\Specification\Sorter\Sorter;
use Spiral\DataGrid\Specification\Value\StringValue;
class PostSchema extends GridSchema
{
public function __construct()
{
// User pagination: limit results to 10 per page
$this->setPaginator(new PagePaginator(10));
// Sorting option: by id
$this->addSorter('id', new Sorter('id'));
// Filter option: find by title matching post input
$this->addFilter('title', new Equals('title', new StringValue()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment