Skip to content

Commit f9102b5

Browse files
committed
Allows to use Relation instead of Builder to generate data
Took 8 minutes
1 parent 5eccc56 commit f9102b5

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

src/DataTableComponent.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Rappasoft\LaravelLivewireTables;
44

5+
use Illuminate\Database\Eloquent\Relations\Relation;
56
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Pagination\LengthAwarePaginator;
78
use Illuminate\Support\Collection;
@@ -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
@@ -2,6 +2,7 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Traits;
44

5+
use Illuminate\Database\Eloquent\Relations\Relation;
56
use Illuminate\Database\Eloquent\Builder;
67
use Rappasoft\LaravelLivewireTables\Utilities\ColumnUtilities;
78
use Rappasoft\LaravelLivewireTables\Views\Column;
@@ -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
@@ -2,6 +2,7 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Traits;
44

5+
use Illuminate\Database\Eloquent\Relations\Relation;
56
use Illuminate\Database\Eloquent\Builder;
67

78
/**
@@ -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()) {

0 commit comments

Comments
 (0)