Skip to content

Commit ecc21d1

Browse files
authored
Merge pull request #281 from rappasoft/develop
v1.6.1
2 parents 5eccc56 + 5abb233 commit ecc21d1

File tree

8 files changed

+179
-12
lines changed

8 files changed

+179
-12
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
44

55
## [Unreleased]
66

7+
## [1.6.1] - 2021-05-13
8+
9+
### Changed
10+
11+
- [Allows to use Relation instead of Builder to generate data](https://github.com/rappasoft/laravel-livewire-tables/pull/279)
12+
713
## [1.6.0] - 2021-05-04
814

915
### Added
@@ -323,8 +329,9 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
323329

324330
- Initial release
325331

326-
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.6.0...development
327-
[1.6.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.4.0...v1.6.0
332+
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.6.1...development
333+
[1.6.1]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.6.0...v1.6.1
334+
[1.6.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.5.1...v1.6.0
328335
[1.5.1]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.4.0...v1.5.1
329336
[1.5.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.4.0...v1.5.0
330337
[1.4.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v1.3.1...v1.4.0

src/DataTableComponent.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Relations\Relation;
67
use Illuminate\Pagination\LengthAwarePaginator;
78
use Illuminate\Support\Collection;
89
use Livewire\Component;
@@ -99,9 +100,9 @@ abstract public function columns(): array;
99100
/**
100101
* The base query with search and filters for the table.
101102
*
102-
* @return Builder
103+
* @return Builder|Relation
103104
*/
104-
abstract public function query(): Builder;
105+
abstract public function query();
105106

106107
/**
107108
* TableComponent constructor.
@@ -124,9 +125,9 @@ public function __construct($id = null)
124125
/**
125126
* Get the rows query builder with sorting applied.
126127
*
127-
* @return Builder
128+
* @return Builder|Relation
128129
*/
129-
public function rowsQuery(): Builder
130+
public function rowsQuery()
130131
{
131132
$this->cleanFilters();
132133

src/Traits/WithBulkActions.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables\Traits;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Relations\Relation;
67

78
/**
89
* Trait WithBulkActions.
@@ -58,13 +59,19 @@ public function resetBulk(): void
5859
$this->selected = [];
5960
}
6061

61-
public function selectedRowsQuery(): Builder
62+
/**
63+
* @return Builder|Relation
64+
*/
65+
public function selectedRowsQuery()
6266
{
6367
return (clone $this->rowsQuery())
6468
->unless($this->selectAll, fn ($query) => $query->whereIn($this->primaryKey, $this->selected));
6569
}
6670

67-
public function getSelectedRowsQueryProperty(): Builder
71+
/**
72+
* @return Builder|Relation
73+
*/
74+
public function getSelectedRowsQueryProperty()
6875
{
6976
return $this->selectedRowsQuery();
7077
}

src/Traits/WithFilters.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables\Traits;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Relations\Relation;
67
use Rappasoft\LaravelLivewireTables\Utilities\ColumnUtilities;
78
use Rappasoft\LaravelLivewireTables\Views\Column;
89
use Rappasoft\LaravelLivewireTables\Views\Filter;
@@ -253,10 +254,10 @@ public function getSearchableColumns() : array
253254
/**
254255
* Apply Search Filter
255256
*
256-
* @param Builder $query
257-
* @return Builder
257+
* @param Builder|Relation $query
258+
* @return Builder|Relation
258259
*/
259-
public function applySearchFilter(Builder $query): Builder
260+
public function applySearchFilter($query)
260261
{
261262
$searchableColumns = $this->getSearchableColumns();
262263

src/Traits/WithSorting.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables\Traits;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Relations\Relation;
67

78
/**
89
* Trait WithSorting.
@@ -34,7 +35,12 @@ public function sortBy(string $field): ?string
3435
return null;
3536
}
3637

37-
public function applySorting(Builder $query): Builder
38+
/**
39+
* @param Builder|Relation $query
40+
*
41+
* @return Builder|Relation
42+
*/
43+
public function applySorting($query)
3844
{
3945
foreach ($this->sorts as $field => $direction) {
4046
if (optional($this->getColumn($field))->hasSortCallback()) {

tests/Http/Livewire/CatsTable.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
4+
namespace Rappasoft\LaravelLivewireTables\Tests\Http\Livewire;
5+
6+
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Eloquent\Relations\Relation;
8+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
9+
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
10+
use Rappasoft\LaravelLivewireTables\Views\Column;
11+
12+
class CatsTable extends DataTableComponent
13+
{
14+
public function query(): Relation
15+
{
16+
/** @var Species $dogSpecimen */
17+
$dogSpecimen = Species::query()->where('name', 'Cat')->first();
18+
19+
return $dogSpecimen->pets()->with('species')->with('breed');
20+
}
21+
22+
public function columns(): array
23+
{
24+
return [
25+
Column::make('Name', 'name')
26+
->searchable(),
27+
Column::make('Age', 'age')
28+
->searchable(function (Builder $query, $search) {
29+
$query->orWhere('age', '=', $search);
30+
}),
31+
Column::make('Last Visit', 'last_visit')
32+
->searchable(),
33+
Column::make('Species', 'species.name')
34+
->searchable(),
35+
Column::make('Breed', 'breed.name')
36+
->searchable(),
37+
];
38+
}
39+
}

tests/Models/Species.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables\Tests\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\HasMany;
67

78
/**
89
* @property int $id
@@ -38,4 +39,9 @@ class Species extends Model
3839
'id',
3940
'name',
4041
];
42+
43+
public function pets(): HasMany
44+
{
45+
return $this->hasMany(Pet::class);
46+
}
4147
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests;
4+
5+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6+
use Illuminate\Support\Collection;
7+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
8+
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\CatsTable;
9+
10+
class RelationshipDataTableComponentTest extends TestCase
11+
{
12+
protected DataTableComponent $table;
13+
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->table = new CatsTable();
19+
}
20+
21+
/** @test */
22+
public function bootstrap_test_datatable(): void
23+
{
24+
$this->assertInstanceOf(DataTableComponent::class, $this->table);
25+
}
26+
27+
/** @test */
28+
public function columns(): void
29+
{
30+
$columns = $this->table->columns();
31+
32+
$this->assertCount(5, $columns);
33+
}
34+
35+
/** @test */
36+
public function rows(): void
37+
{
38+
$rows = $this->table->rows;
39+
40+
$this->assertInstanceOf(LengthAwarePaginator::class, $rows);
41+
$this->assertEquals(2, $this->table->getRowsProperty()->total());
42+
}
43+
44+
/** @test */
45+
public function pagination_default(): void
46+
{
47+
$this->assertInstanceOf(LengthAwarePaginator::class, $this->table->rows);
48+
$this->assertEquals(10, $this->table->perPage);
49+
$this->assertTrue($this->table->paginationEnabled);
50+
$this->assertTrue($this->table->showPerPage);
51+
}
52+
53+
/** @test */
54+
public function pagination(): void
55+
{
56+
$this->table->perPage = 1;
57+
$this->assertEquals(1, $this->table->rows->currentPage());
58+
$this->assertEquals(1, $this->table->rows->count());
59+
$this->assertEquals(2, $this->table->rows->lastPage());
60+
}
61+
62+
/** @test */
63+
public function pagination_disabled(): void
64+
{
65+
$this->table->paginationEnabled = false;
66+
$this->table->perPage = 2;
67+
$this->assertInstanceOf(Collection::class, $this->table->rows);
68+
$this->assertCount(2, $this->table->rows);
69+
}
70+
71+
/** @test */
72+
public function search_filter(): void
73+
{
74+
$this->table->filters['search'] = 'Cartman';
75+
$this->assertEquals(1, $this->table->getRowsProperty()->total());
76+
}
77+
78+
/** @test */
79+
public function search_filter_reset(): void
80+
{
81+
$this->table->filters['search'] = 'Cartman';
82+
$this->table->resetFilters();
83+
$this->assertEquals(1, $this->table->rows->total());
84+
}
85+
86+
/** @test */
87+
public function search_filter_remove(): void
88+
{
89+
$this->table->filters['search'] = 'Cartman';
90+
$this->table->removeFilter('search');
91+
$this->assertEquals(2, $this->table->rows->total());
92+
}
93+
94+
/** @test */
95+
public function search_filter_callback(): void
96+
{
97+
$this->table->filters['search'] = '22';
98+
$this->assertEquals(1, $this->table->getRowsProperty()->total());
99+
}
100+
}

0 commit comments

Comments
 (0)