Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function up(): void
$table->foreignIdFor(Author::class)->nullable();
$table->string('name');
$table->string('description');
$table->string('page_count');
$table->foreignIdFor(Product::class)->nullable();
$table->timestamps();
});
Expand Down
2 changes: 2 additions & 0 deletions tests/App/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class Book extends Model
protected $filterFields = [
'name', // field
'description', // field
'page_count', // field
];

protected $fillable = [
'name',
'description',
'page_count',
];

public function author(): BelongsTo
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/FilterableByMultipleFieldInRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,37 @@ public function setUp(): void
$author->books()->create([
'name' => 'A Game of Thrones',
'description' => 'A Game of Thrones is the first novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.',
'page_count' => 100,
]);
$author->books()->create([
'name' => 'A Clash of Kings',
'description' => 'A Clash of Kings is the second novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.',
'page_count' => 200,
]);
$author->books()->create([
'name' => 'A Storm of Swords',
'description' => 'A Storm of Swords is the third novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.',
'page_count' => 300,
]);
$author->books()->create([
'name' => 'A Feast for Crows',
'description' => 'A Feast for Crows is the fourth novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.',
'page_count' => 400,
]);
$author->books()->create([
'name' => 'A Dance with Dragons',
'description' => 'A Dance with Dragons is the fifth novel in A Song of Ice and Fire, a series of fantasy novels by the American author George R. R. Martin.',
'page_count' => 500,
]);
$author->books()->create([
'name' => 'The Winds of Winter',
'description' => 'The Winds of Winter is the planned sixth novel in the epic fantasy series A Song of Ice and Fire by American writer George R. R. Martin.',
'page_count' => 600,
]);
$author->books()->create([
'name' => 'A Dream of Spring',
'description' => 'A Dream of Spring is the planned seventh novel in the epic fantasy series A Song of Ice and Fire by American writer George R. R. Martin.',
'page_count' => 700,
]);

// author
Expand All @@ -57,14 +64,17 @@ public function setUp(): void
$author->books()->create([
'name' => 'The Hobbit',
'description' => 'The Hobbit, or There and Back Again is a children\'s fantasy novel by English author J. R. R. Tolkien.',
'page_count' => 100,
]);
$author->books()->create([
'name' => 'The Lord of the Rings',
'description' => 'The Lord of the Rings is an epic high-fantasy novel by the English author and scholar J. R. R. Tolkien.',
'page_count' => 200,
]);
$author->books()->create([
'name' => 'The Silmarillion',
'description' => 'The Silmarillion is a collection of mythopoeic works by English writer J. R. R. Tolkien.',
'page_count' => 300,
]);
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Feature/RelationFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,50 @@ public function it_can_filter_by_has_one_relation(): void
$product->book()->create([
'name' => 'book',
'description' => 'book for product',
'page_count' => 100,
]);

$response = $this->getJson('/products?filters[book][name][$eq]=book');

$response->assertOk();
$response->assertJsonCount(1);
}

/** @test */
public function it_can_filter_by_multiple_fields_on_one_relationship(): void
{
$product1 = Product::factory([
'name' => 'Laravel Purity',
])->create();

$product2 = Product::factory([
'name' => 'Laravel Purity',
])->create();

//exact match, should return
$product1->book()->create([
'name' => 'book',
'description' => 'book for product',
'page_count' => 100,
]);

//only matching name should not return
$product2->book()->create([
'name' => 'book',
'description' => 'book for product',
'page_count' => 200,
]);

//only matching page_count should not return
$product2->book()->create([
'name' => 'book2',
'description' => 'book2 for product2',
'page_count' => 100,
]);

$response = $this->getJson('/products?filters[book][name][$eq]=book&filters[book][page_count][$eq]=100');

$response->assertOk();
$response->assertJsonCount(1);
}
}
2 changes: 2 additions & 0 deletions tests/Feature/RenameFilterableFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ public function available_filter_fields_work_with_renamed_filter_fields(): void
$book->create([
'name' => 'name_1',
'description' => 'description_1',
'page_count' => 100,
])->create([
'name' => 'name_1',
'description' => 'description_2',
'page_count' => 200,
]);

Route::get('/books', function () use ($book) {
Expand Down