Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Make any column searchable with like in PostgreSQL (and fix Global Search in Laravel Nova) #25698

Merged
merged 2 commits into from
Sep 19, 2018
Merged
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
33 changes: 33 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Database\Query\Grammars;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Database\Query\Builder;

class PostgresGrammar extends Grammar
Expand All @@ -20,6 +21,38 @@ class PostgresGrammar extends Grammar
'is distinct from', 'is not distinct from',
];

/**
* {@inheritdoc}
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBasic(Builder $query, $where)
{
// Special treatment for like clauses. In order to make
// any column searchable, we need to convert it to text.
if (Str::contains(strtolower($where['operator']), 'like')) {
return $this->whereBasicLike($query, $where);
}

return parent::whereBasic($query, $where);
}

/**
* Compile "where like", "where ilike", "where not like" and "where not ilike" clauses.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBasicLike(Builder $query, $where)
{
$value = $this->parameter($where['value']);

return $this->wrap($where['column']).'::text '.$where['operator'].' '.$value;
}

/**
* Compile a "where date" clause.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,34 @@ public function testWhereTimePostgres()
$this->assertEquals([0 => '22:00'], $builder->getBindings());
}

public function testWhereLikePostgres()
{
$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'like', '1');
$this->assertEquals('select * from "users" where "id"::text like ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'LIKE', '1');
$this->assertEquals('select * from "users" where "id"::text LIKE ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'ilike', '1');
$this->assertEquals('select * from "users" where "id"::text ilike ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'not like', '1');
$this->assertEquals('select * from "users" where "id"::text not like ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', 'not ilike', '1');
$this->assertEquals('select * from "users" where "id"::text not ilike ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());
}

public function testWhereDateSqlite()
{
$builder = $this->getSQLiteBuilder();
Expand Down