From f799a380fb9d10b61c942b964c94a93faa70ae8a Mon Sep 17 00:00:00 2001 From: aldozorzi Date: Thu, 12 Sep 2019 17:21:34 +0200 Subject: [PATCH] Added IN statement for WAHERE condition Added whereIn macro to Laravel\Scout\Builder Class --- README.md | 13 ++++++++++++- src/Engines/Modes/Mode.php | 16 +++++++++++++--- src/Providers/MySQLScoutServiceProvider.php | 8 ++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2806936..7615500 100644 --- a/README.md +++ b/README.md @@ -87,11 +87,22 @@ Simple constraints can be applied using the `where()` builder method `$beers = App\Drink::search('beer')->where('in_stock', 1)->get();` -The following operators can be applied to the `WHERE` statements: `<> != = <= < >= >` +The following operators can be applied to the `WHERE` statements: `<> != = <= < >= > IN` (`=` will be used if no operator is specified) `$beers = App\Drink::search('beer')->where('abv >', 10)->get();` +`$beers = App\Drink::search('beer')->where('abv IN', '(8,9,10)')->get();` + +Thanks to IN statement it's possible to apply complex constraints. Get a Collection and call the `pluck` method, then pass the result to `whereIn` Scout Builder method: + +```php + + $matching = App\Drink::whereHas('ownglasses')->pluck('id'); + $beers = App\Drink::search('beer')->whereIn('id',$matching)->get(); + +``` + For more usage information see the [Laravel Scout Documentation](https://laravel.com/docs/5.3/scout). Modes
diff --git a/src/Engines/Modes/Mode.php b/src/Engines/Modes/Mode.php index 8aa57ef..1c7366b 100644 --- a/src/Engines/Modes/Mode.php +++ b/src/Engines/Modes/Mode.php @@ -36,8 +36,18 @@ protected function buildWheres(Builder $builder) $value = $parsedWhere[2]; if ($value !== null) { - $this->whereParams[$field] = $value; - $queryString .= "$field $operator ? AND "; + if($operator == 'IN') + { + if($value == '()') + $queryString .= "0=1 AND "; + else + $queryString .= "$field $operator $value AND "; + } + else + { + $this->whereParams[$field] = $value; + $queryString .= "$field $operator ? AND "; + } } else { $queryString .= "$field IS NULL AND "; } @@ -48,7 +58,7 @@ protected function buildWheres(Builder $builder) private function parseWheres($wheres) { - $pattern = '/([A-Za-z_]+[A-Za-z_0-9]?)[ ]?(<>|!=|=|<=|<|>=|>)/'; + $pattern = '/([A-Za-z_]+[A-Za-z_0-9]?)[ ]?(<>|!=|=|<=|<|>=|>|IN)/'; $result = array(); foreach ($wheres as $field => $value) { diff --git a/src/Providers/MySQLScoutServiceProvider.php b/src/Providers/MySQLScoutServiceProvider.php index 42e7343..1713829 100644 --- a/src/Providers/MySQLScoutServiceProvider.php +++ b/src/Providers/MySQLScoutServiceProvider.php @@ -11,6 +11,8 @@ use Yab\MySQLScout\Services\IndexService; use Yab\MySQLScout\Commands\ManageIndexes; +use Laravel\Scout\Builder; + class MySQLScoutServiceProvider extends ServiceProvider { /** @@ -26,6 +28,12 @@ public function boot() $this->app->make(EngineManager::class)->extend('mysql', function () { return new MySQLEngine(app(ModeContainer::class)); + }); + + // Add Macro WhereIn + Builder::macro('whereIn', function ($field,$collection) { + $this->where($field.' IN ','('.(implode(',',$collection->toArray())).')'); + return $this; }); }