Skip to content

Commit e82678a

Browse files
committed
adding search
1 parent f9c0323 commit e82678a

File tree

3 files changed

+118
-8
lines changed

3 files changed

+118
-8
lines changed

src/DataTableComponent.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,19 @@ public function rowsQuery(): Builder
123123
{
124124
$this->cleanFilters();
125125

126-
return $this->applySorting($this->query());
127-
}
126+
$query = $this->query();
128127

129-
/**
130-
* @return Builder
131-
*/
132-
public function getRowsQueryProperty(): Builder
133-
{
134-
return $this->rowsQuery();
128+
// sorting?
129+
if (method_exists($this, 'applySorting')) {
130+
$query = $this->applySorting($query);
131+
}
132+
133+
// searching?
134+
if (method_exists($this, 'applySearchFilter')) {
135+
$query = $this->applySearchFilter($query);
136+
}
137+
138+
return $query;
135139
}
136140

137141
/**

src/Traits/WithFilters.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Traits;
44

5+
use Illuminate\Database\Eloquent\Builder;
6+
use Rappasoft\LaravelLivewireTables\Utilities\ColumnUtilities;
7+
use Rappasoft\LaravelLivewireTables\Views\Column;
8+
59
/**
610
* Trait WithFilters.
711
*/
@@ -183,4 +187,75 @@ public function getFilterOptions(string $filter): array
183187
{
184188
return array_filter(array_keys($this->filters()[$filter]->options() ?? []));
185189
}
190+
191+
/**
192+
* Collects columns with $searchable = true
193+
*
194+
* @return Column[]
195+
*/
196+
public function getSearchableColumns() : array
197+
{
198+
return array_filter($this->columns(), function(Column $column) {
199+
return $column->isSearchable();
200+
});
201+
}
202+
203+
/**
204+
* Apply Search Filter
205+
*
206+
* @param Builder $query
207+
* @return Builder
208+
*/
209+
public function applySearchFilter(Builder $query): Builder
210+
{
211+
if ($this->hasFilter('search')) {
212+
213+
// get search value
214+
$search = $this->getFilter('search');
215+
216+
// trim
217+
$search = trim($search);
218+
219+
// group search conditions together
220+
$query->where(function (Builder $subQuery) use ($search, $query) {
221+
222+
foreach ($this->getSearchableColumns() as $column) {
223+
224+
// does this column have an alias or relation?
225+
$hasRelation = ColumnUtilities::hasRelation($column->column());
226+
227+
// let's try to map this column to a selected column
228+
$selectedColumn = ColumnUtilities::mapToSelected($column->column(), $query);
229+
230+
// if the column has a search callback, just use that
231+
if ($column->searchCallback) {
232+
233+
// call the callback
234+
($column->searchCallback)($query, $search);
235+
236+
// if the column isn't a relation or if it was previously selected
237+
} elseif (! $hasRelation || $selectedColumn) {
238+
239+
// we can use a simple where clause
240+
$subQuery->orWhere($selectedColumn ?? $column->column(), 'like', '%' . $search . '%');
241+
242+
} else {
243+
244+
// parse the column
245+
$relationName = ColumnUtilities::parseRelation($column->column());
246+
$fieldName = ColumnUtilities::parseField($column->column());
247+
248+
// we use whereHas which can work with unselected relations
249+
$subQuery->orWhereHas($relationName, function (Builder $hasQuery) use ($fieldName, $column, $search) {
250+
$hasQuery->where($fieldName, 'like', '%' . $search . '%');
251+
});
252+
253+
}
254+
}
255+
});
256+
257+
}
258+
259+
return $query;
260+
}
186261
}

src/Views/Column.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class Column
2929
*/
3030
public $sortCallback;
3131

32+
/**
33+
* @var bool
34+
*/
35+
public bool $searchable = false;
36+
37+
/**
38+
* @var callable
39+
*/
40+
public $searchCallback;
41+
3242
/**
3343
* @var string|null
3444
*/
@@ -97,6 +107,14 @@ public function isSortable(): bool
97107
return $this->sortable === true;
98108
}
99109

110+
/**
111+
* @return bool
112+
*/
113+
public function isSearchable(): bool
114+
{
115+
return $this->searchable === true;
116+
}
117+
100118
/**
101119
* @return bool
102120
*/
@@ -117,6 +135,19 @@ public function sortable($callback = null): self
117135
return $this;
118136
}
119137

138+
/**
139+
* @param callable|null $callback
140+
* @return $this
141+
*/
142+
public function searchable(callable $callback = null): self
143+
{
144+
$this->searchable = true;
145+
146+
$this->searchCallback = $callback;
147+
148+
return $this;
149+
}
150+
120151
/**
121152
* @param string $class
122153
*

0 commit comments

Comments
 (0)