Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Added IN statement for WHERE condition and Added whereIn macro to Laravel\Scout\Builder Class #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div id="modes"></div>
Expand Down
16 changes: 13 additions & 3 deletions src/Engines/Modes/Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ";
}
Expand All @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/Providers/MySQLScoutServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Yab\MySQLScout\Services\IndexService;
use Yab\MySQLScout\Commands\ManageIndexes;

use Laravel\Scout\Builder;

class MySQLScoutServiceProvider extends ServiceProvider
{
/**
Expand All @@ -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;
});
}

Expand Down