searchable
searchable copied to clipboard
Laravel 5.2 Support
Pagination not working... Gives error if search query returns zero results : ErrorException in Collection.php line 1040: Undefined offset: 0
+1
+1
+1
+1
WARNING: this is not a solution! This works for some cases and doesn't work for others. Please search for other solutions, maybe here https://github.com/nicolaslopezj/searchable/issues/73
I believe I have the same issue with pagination. In laravel 5.2 when using pagination bindings are lost and query throws an exception:
SQLSTATE[HY000]: General error: 2031 (SQL: select count(*) as aggregate from (select ...
Searchable adds bindings for select
group, but then in https://github.com/illuminate/database/blob/master/Query/Builder.php#L1548 bindings are temporary removed for select group so query throws exception.
protected function backupFieldsForCount()
{
foreach (['orders', 'limit', 'offset', 'columns'] as $field) {
$this->backups[$field] = $this->{$field};
$this->{$field} = null;
}
foreach (['order', 'select'] as $key) {
$this->bindingBackups[$key] = $this->bindings[$key];
$this->bindings[$key] = [];
}
}
So I've replaced select
to where
in https://github.com/nicolaslopezj/searchable/blob/master/src/SearchableTrait.php#L308 and pagination query started to work.
protected function addBindingsToQuery(Builder $query, array $bindings) {
$count = $this->getDatabaseDriver() != 'mysql' ? 2 : 1;
for ($i = 0; $i < $count; $i++) {
foreach($bindings as $binding) {
$type = $i == 0 ? 'where' : 'having';
$query->addBinding($binding, $type);
}
}
}
I didn't create PR because not sure the reason why select
was selected there.
+1 for this
+1
+1
+1
+1
Hi, nicolaslopezj. I know this way is not smart but I guess there's no way to use raw query like this because this issue is caused by laravel core.
protected function getSearchQuery(Builder $query, $column, $relevance, array $words, $relevance_multiplier, $pre_word = '', $post_word = '') { $like_comparator = $this->getDatabaseDriver() == 'pgsql' ? 'ILIKE' : 'LIKE'; $cases = []; foreach ($words as $word) { $case = $this->getCaseCompare($column, $like_comparator, $relevance * $relevance_multiplier); $cases[] = str_replace( '?', DB::connection()->getPdo()->quote($pre_word . $word . $post_word), $case ); } return implode(' + ', $cases); }
@nicolaslopezj plz solve this problem. It is destroying my project.
thanks @antongorodezkiy.. comment on Feb 24 works for now..
#126 should solve the problem