From 20c54de5b7047c385b9175bc21ada01243fabd98 Mon Sep 17 00:00:00 2001 From: Wun Chiou Date: Mon, 19 Oct 2015 13:14:06 -0500 Subject: [PATCH] Add many-to-many polymorphic capability to joins. This checks to see if the values in `$this->searchable['joins']` have a 3rd & 4th value and, if so, implements those as a where clause on the leftJoin. This allows you to join many-to-many polymorphic relations with something like this: ```php protected $searchable = [ 'columns' => [ 'posts.title' => 15, 'posts.description' => 10, 'tags.name' => 7 ], 'joins' => [ 'taggables' => ['posts.id','taggables.taggable_id','taggables.taggable_type','App\Post'], 'tags' => ['taggables.tag_id','tags.id'], ], ]; ``` --- src/SearchableTrait.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/SearchableTrait.php b/src/SearchableTrait.php index 93d1103..7dd37c6 100644 --- a/src/SearchableTrait.php +++ b/src/SearchableTrait.php @@ -137,9 +137,13 @@ protected function getJoins() */ protected function makeJoins(Builder $query) { - foreach ($this->getJoins() as $table => $keys) - { - $query->leftJoin($table, $keys[0], '=', $keys[1]); + foreach ($this->getJoins() as $table => $keys) { + $query->leftJoin($table, function ($join) use ($keys) { + $join->on($keys[0], '=', $keys[1]); + if (array_key_exists(2, $keys) && array_key_exists(3, $keys)) { + $join->where($keys[2], '=', $keys[3]); + } + }); } }