Skip to content

Commit 5ea420e

Browse files
committed
Updates and changelog
1 parent ff10f30 commit 5ea420e

File tree

7 files changed

+96
-103
lines changed

7 files changed

+96
-103
lines changed

CHANGELOG.md

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

55
## [Unreleased]
66

7+
### Added
8+
9+
- Added searchable() to columns (https://github.com/rappasoft/laravel-livewire-tables/pull/233)
10+
711
### Changed
812

913
- Fixed offline indicators to display block

src/DataTableComponent.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,10 @@ public function rowsQuery(): Builder
125125

126126
$query = $this->query();
127127

128-
// sorting?
129128
if (method_exists($this, 'applySorting')) {
130129
$query = $this->applySorting($query);
131130
}
132131

133-
// searching?
134132
if (method_exists($this, 'applySearchFilter')) {
135133
$query = $this->applySearchFilter($query);
136134
}

src/Traits/WithFilters.php

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ public function getFilterOptions(string $filter): array
195195
*/
196196
public function getSearchableColumns() : array
197197
{
198-
return array_filter($this->columns(), function (Column $column) {
199-
return $column->isSearchable();
200-
});
198+
return array_filter($this->columns(), fn (Column $column) => $column->isSearchable());
201199
}
202200

203201
/**
@@ -208,48 +206,40 @@ public function getSearchableColumns() : array
208206
*/
209207
public function applySearchFilter(Builder $query): Builder
210208
{
211-
if ($this->hasFilter('search')) {
209+
$searchableColumns = $this->getSearchableColumns();
212210

213-
// get search value
214-
$search = $this->getFilter('search');
211+
if ($this->hasFilter('search') && count($searchableColumns)) {
212+
$search = trim($this->getFilter('search'));
215213

216-
// trim
217-
$search = trim($search);
218-
219-
// group search conditions together
220-
$query->where(function (Builder $subQuery) use ($search, $query) {
221-
foreach ($this->getSearchableColumns() as $column) {
222-
223-
// does this column have an alias or relation?
214+
// Group search conditions together
215+
$query->where(function (Builder $subQuery) use ($search, $query, $searchableColumns) {
216+
foreach ($searchableColumns as $column) {
217+
// Does this column have an alias or relation?
224218
$hasRelation = ColumnUtilities::hasRelation($column->column());
225219

226-
// let's try to map this column to a selected column
220+
// Let's try to map this column to a selected column
227221
$selectedColumn = ColumnUtilities::mapToSelected($column->column(), $query);
228222

229-
// if the column has a search callback, just use that
223+
// If the column has a search callback, just use that
230224
if ($column->searchCallback) {
231-
232-
// call the callback
225+
// Call the callback
233226
($column->searchCallback)($query, $search);
234-
235-
// if the column isn't a relation or if it was previously selected
236-
} elseif (! $hasRelation || $selectedColumn) {
227+
} elseif (! $hasRelation || $selectedColumn) { // If the column isn't a relation or if it was previously selected
237228
$whereColumn = $selectedColumn ?? $column->column();
238229

239-
// @todo: skip aggregates
230+
// TODO: Skip Aggregates
240231
if (! $hasRelation && $query instanceof Builder) {
241232
$whereColumn = $query->getModel()->getTable() . '.' . $whereColumn;
242233
}
243234

244-
// we can use a simple where clause
235+
// We can use a simple where clause
245236
$subQuery->orWhere($whereColumn, 'like', '%' . $search . '%');
246237
} else {
247-
248-
// parse the column
238+
// Parse the column
249239
$relationName = ColumnUtilities::parseRelation($column->column());
250240
$fieldName = ColumnUtilities::parseField($column->column());
251241

252-
// we use whereHas which can work with unselected relations
242+
// We use whereHas which can work with unselected relations
253243
$subQuery->orWhereHas($relationName, function (Builder $hasQuery) use ($fieldName, $column, $search) {
254244
$hasQuery->where($fieldName, 'like', '%' . $search . '%');
255245
});

src/Utilities/ColumnUtilities.php

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
namespace Rappasoft\LaravelLivewireTables\Utilities;
44

55
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6-
use Illuminate\Database\Query\Builder as Builder;
6+
use Illuminate\Database\Query\Builder;
77
use Illuminate\Support\Str;
88

9+
/**
10+
* Class ColumnUtilities
11+
*
12+
* @package Rappasoft\LaravelLivewireTables\Utilities
13+
*/
914
class ColumnUtilities
1015
{
1116
/**
@@ -14,7 +19,7 @@ class ColumnUtilities
1419
* @param $column
1520
* @return bool
1621
*/
17-
public static function hasRelation($column)
22+
public static function hasRelation($column): bool
1823
{
1924
return Str::contains($column, '.');
2025
}
@@ -25,7 +30,7 @@ public static function hasRelation($column)
2530
* @param $column
2631
* @return string
2732
*/
28-
public static function parseRelation($column)
33+
public static function parseRelation($column): string
2934
{
3035
return Str::beforeLast($column, '.');
3136
}
@@ -36,7 +41,7 @@ public static function parseRelation($column)
3641
* @param $column
3742
* @return string
3843
*/
39-
public static function parseField($column)
44+
public static function parseField($column): string
4045
{
4146
return Str::afterLast($column, '.');
4247
}
@@ -48,9 +53,9 @@ public static function parseField($column)
4853
* @param $searchColumns
4954
* @return bool
5055
*/
51-
public static function hasMatch($column, $searchColumns)
56+
public static function hasMatch($column, $searchColumns): bool
5257
{
53-
return array_search($column, $searchColumns ?? []) !== false;
58+
return in_array($column, $searchColumns ?? [], true);
5459
}
5560

5661
/**
@@ -60,42 +65,44 @@ public static function hasMatch($column, $searchColumns)
6065
* @param $searchColumns
6166
* @return bool
6267
*/
63-
public static function hasWildcardMatch($column, $searchColumns)
68+
public static function hasWildcardMatch($column, $searchColumns): bool
6469
{
6570
return count(array_filter($searchColumns ?? [], function ($searchColumn) use ($column) {
66-
67-
// match wildcards such as * or table.*
71+
// Match wildcards such as * or table.*
6872
$hasWildcard = Str::endsWith($searchColumn, '*');
6973

70-
// if no wildcard, skip
74+
// If no wildcard, skip
7175
if (! $hasWildcard) {
7276
return false;
7377
}
7478

7579
if (! self::hasRelation($column)) {
7680
return true;
77-
} else {
78-
$selectColumnPrefix = self::parseRelation($searchColumn);
79-
$columnPrefix = self::parseRelation($column);
80-
81-
return $selectColumnPrefix === $columnPrefix;
8281
}
82+
83+
$selectColumnPrefix = self::parseRelation($searchColumn);
84+
$columnPrefix = self::parseRelation($column);
85+
86+
return $selectColumnPrefix === $columnPrefix;
8387
})) > 0;
8488
}
8589

8690
/**
87-
* @param EloquentBuilder|Builder $queryBuilder
88-
* @return null
91+
* @param null $queryBuilder
92+
*
93+
* @return array|null
8994
*/
90-
public static function columnsFromBuilder($queryBuilder = null)
95+
public static function columnsFromBuilder($queryBuilder = null): ?array
9196
{
9297
if ($queryBuilder instanceof EloquentBuilder) {
9398
return $queryBuilder->getQuery()->columns;
94-
} elseif ($queryBuilder instanceof Builder) {
99+
}
100+
101+
if ($queryBuilder instanceof Builder) {
95102
return $queryBuilder->columns;
96-
} else {
97-
return null;
98103
}
104+
105+
return null;
99106
}
100107

101108
/**
@@ -105,48 +112,48 @@ public static function columnsFromBuilder($queryBuilder = null)
105112
* @param $queryBuilder
106113
* @return string
107114
*/
108-
public static function mapToSelected($column, $queryBuilder)
115+
public static function mapToSelected($column, $queryBuilder): ?string
109116
{
110-
// grab select
117+
// Grab select
111118
$select = self::columnsFromBuilder($queryBuilder);
112119

113-
// can't match anything if no select
120+
// Can't match anything if no select
114121
if (is_null($select)) {
115122
return null;
116123
}
117124

118-
// search builder select for a match
125+
// Search builder select for a match
119126
$hasMatch = self::hasMatch($column, $select);
120127

121128
// example 2 - match
122129
// column: service_statuses.name
123130
// select: service_statuses.name
124131
// maps to: service_statuses.name
125132

126-
// if we found a match, lets use that instead of searching relations
133+
// If we found a match, lets use that instead of searching relations
127134
if ($hasMatch) {
128135
return $column;
129136
}
130137

131-
// search builder select for a wildcard match
138+
// Search builder select for a wildcard match
132139
$hasWildcardMatch = self::hasWildcardMatch($column, $select);
133140

134141
// example 3 - wildcard match
135142
// column: service_statuses.name
136143
// select: service_statuses.*
137144
// maps to: service_statuses.name
138145

139-
// if we found a wildcard match, lets use that instead of matching relations
146+
// If we found a wildcard match, lets use that instead of matching relations
140147
if ($hasWildcardMatch) {
141148
return $column;
142149
}
143150

144-
// split the relation and field
151+
// Split the relation and field
145152
$hasRelation = self::hasRelation($column);
146153
$relationName = self::parseRelation($column);
147154
$fieldName = self::parseField($column);
148155

149-
// we know there is a relation and we know it doesn't match any of the
156+
// We know there is a relation and we know it doesn't match any of the
150157
// select columns. Let's try to grab the table name for the relation
151158
// and see if that matches something in the select
152159
//
@@ -155,49 +162,45 @@ public static function mapToSelected($column, $queryBuilder)
155162
// select: service_statuses.name
156163
// maps to: service_statuses.name
157164

158-
// if we didn't previously match the column and there isn't a relation
165+
// If we didn't previously match the column and there isn't a relation
159166
if (! $hasRelation) {
160-
161-
// there's nothing else to do
167+
// There's nothing else to do
162168
return null;
169+
}
163170

164-
// this is easiest when using the eloquent query builder
165-
} elseif ($queryBuilder instanceof EloquentBuilder) {
171+
// This is easiest when using the eloquent query builder
172+
if ($queryBuilder instanceof EloquentBuilder) {
166173
$relation = $queryBuilder->getRelation($relationName);
167174
$possibleTable = $relation->getModel()->getTable();
168175
} elseif ($queryBuilder instanceof Builder) {
169-
170-
// @todo: possible ways to do this?
176+
// TODO: Possible ways to do this?
171177
$possibleTable = null;
172178
} else {
173-
174-
// we would have already returned before this is possible
179+
// We would have already returned before this is possible
175180
$possibleTable = null;
176181
}
177182

178-
// if we found a possible table
183+
// If we found a possible table
179184
if (! is_null($possibleTable)) {
180-
181-
// build possible selected column
185+
// Build possible selected column
182186
$possibleSelectColumn = $possibleTable . '.' . $fieldName;
183187

184188
$possibleMatch = self::hasMatch($possibleSelectColumn, $select);
185189

186-
// if we found a possible match for a relation to an already selected
187-
// column, let's use that
190+
// If we found a possible match for a relation to an already selected column, let's use that
188191
if ($possibleMatch) {
189192
return $possibleSelectColumn;
190193
}
191194

192195
$possibleWildcardMatch = self::hasWildcardMatch($possibleSelectColumn, $select);
193196

194-
// ditto with a possible wildcard match
197+
// Ditto with a possible wildcard match
195198
if ($possibleWildcardMatch) {
196199
return $possibleSelectColumn;
197200
}
198201
}
199202

200-
// we couldn't match to a selected column
203+
// We couldn't match to a selected column
201204
return null;
202205
}
203206
}

0 commit comments

Comments
 (0)