From fdb18fa0691dad24924a5bb48e7b1c82717c0392 Mon Sep 17 00:00:00 2001 From: Enaah Date: Thu, 2 Sep 2021 21:15:22 +0200 Subject: [PATCH] update/`applySearchFilter()` (virtual columns) Extends the `applySearchFilter($query)`-function to check if the name of the `$whereColumn` exists on the (parent) schema. If the column (name) exists, add the table-name as a prefix `[tableName].[columnName]` just like before. If the column name does not exist, just take the column name `[columnName]`. If it still does not exists (as a **virtual column**), it will fail just like before. --- src/Traits/WithFilters.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Traits/WithFilters.php b/src/Traits/WithFilters.php index 9b33ea655..0722247aa 100644 --- a/src/Traits/WithFilters.php +++ b/src/Traits/WithFilters.php @@ -3,11 +3,12 @@ namespace Rappasoft\LaravelLivewireTables\Traits; use DateTime; +use Illuminate\Support\Facades\Schema; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Rappasoft\LaravelLivewireTables\Utilities\ColumnUtilities; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Filter; +use Illuminate\Database\Eloquent\Relations\Relation; +use Rappasoft\LaravelLivewireTables\Utilities\ColumnUtilities; /** * Trait WithFilters. @@ -316,7 +317,8 @@ public function applySearchFilter($query) // TODO: Skip Aggregates if (! $hasRelation) { - $whereColumn = $query->getModel()->getTable() . '.' . $whereColumn; + $whereColumn = Schema::hasColumn($query->getModel()->getTable(), $whereColumn) ? $query->getModel()->getTable() . '.' . $whereColumn : $whereColumn; + } // We can use a simple where clause