From 69c46773bda6bd0422459a62bb3e24d026ce7a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geovane=20Kr=C3=BCger?= Date: Tue, 24 Aug 2021 20:14:21 -0300 Subject: [PATCH] Update Model.php for nested relations Updated model.php so that the searchRelation() method works with polymorphic relationships, and for a nested relationship. --- src/Helpers/Model.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Helpers/Model.php b/src/Helpers/Model.php index c15da559..b69ff512 100644 --- a/src/Helpers/Model.php +++ b/src/Helpers/Model.php @@ -270,14 +270,20 @@ private function makeRelation(): void } if ($this->query->getRelation($table)) { - foreach ($relation as $column) { - if (!Schema::hasColumn($this->query->getModel()->getTable(), $column)) { - return; + foreach ($relation as $nestedTable => $column) { + if (is_array($column)) { + if ($this->query->getRelation($table)->getRelation($nestedTable)){ + foreach ($column as $nestedColumn) { + $this->query = $this->query->orWhereHas($table.'.'.$nestedTable, function (Builder $query) use ($nestedColumn) { + $query->where($nestedColumn, 'like', '%' . $this->search . '%'); + }); + } + } + } else { + $this->query = $this->query->orWhereHas($table, function (Builder $query) use ($column) { + $query->where($column, 'like', '%' . $this->search . '%'); + }); } - - $this->query = $this->query->orWhereHas($table, function (Builder $query) use ($column) { - $query->where($column, 'like', '%' . $this->search . '%'); - }); } } }