searchable icon indicating copy to clipboard operation
searchable copied to clipboard

Scope problem

Open ozgurkaragoz opened this issue 9 years ago • 7 comments

Hi,

I am using searchable with own publishedScope (Global Scope) on laravel 5.2.5

PublishedScope

$builder->where('created_at', '<', Carbon::now());

When used like:

Watch::search($searchQuery)->get();

I get error.

When used like:

Watch::withoutGlobalScopes([PublishedScope::class])->search($searchQuery)->get();

No error it is works. But i must use that scope.

Model:

protected $searchable = [
        'columns' => [
            'watch_translations.title' => 10,
            'watch_translations.excerpt' => 5,
            'watch_translations.content' => 3,
        ],
        'joins' => [
            'watch_translations' => ['watches.id','watch_translations.watch_id'],
        ],
    ];

Error:

SQLSTATE[HY093]: Invalid parameter number (SQL: select * from (select `watches`.*, avg((case when LOWER(`watch_translations`.`title`) LIKE searchkeyword then 150 else 0 end) + (case when LOWER(`watch_translations`.`title`) LIKE searchkeyword% then 50 else 0 end) + (case when LOWER(`watch_translations`.`title`) LIKE %searchkeyword% then 10 else 0 end) + (case when LOWER(`watch_translations`.`excerpt`) LIKE searchkeyword then 75 else 0 end) + (case when LOWER(`watch_translations`.`excerpt`) LIKE searchkeyword% then 25 else 0 end) + (case when LOWER(`watch_translations`.`excerpt`) LIKE %searchkeyword% then 5 else 0 end) + (case when LOWER(`watch_translations`.`content`) LIKE searchkeyword then 45 else 0 end) + (case when LOWER(`watch_translations`.`content`) LIKE searchkeyword% then 15 else 0 end) + (case when LOWER(`watch_translations`.`content`) LIKE %searchkeyword% then 3 else 0 end)) as relevance from `watches` left join `watch_translations` on `watches`.`id` = `watch_translations`.`watch_id` where `watches`.`deleted_at` is null and `created_at` < 2015-12-31 11:54:59 group by `watches`.`id`, `watch_translations`.`title`, `watch_translations`.`excerpt`, `watch_translations`.`content` having relevance > 4.50 order by `relevance` desc, `position` asc) as `watches` where `watches`.`deleted_at` is null and `created_at` < ? order by `position` asc)```

ozgurkaragoz avatar Dec 31 '15 09:12 ozgurkaragoz

I am experiencing this issue too. Did you find any solution to this?

Another bad solution I am trying now is to disable all global scopes, and apply them manually using regular scopes, but, it'd be nice to have global scopes running :joy:

mgarciadelojo avatar Feb 10 '16 08:02 mgarciadelojo

I get same issue (with global scope).

Do you guys have solution to avoid this problem?

tarik817 avatar May 17 '16 11:05 tarik817

@tarik817 I ended up doing this (using repository pattern):

    /**
     * Search functionality.
     *
     * @param string $query
     * @param int $limit
     *
     * @return Collection
     */
    public function searchBy($query, $limit = 50)
    {
        return $this->model
            ->withoutGlobalScopes()
            ->applyScope1()
            ->applyScope2()
            ->search($query)
            ->limit($limit)
            ->get();
    }

mgarciadelojo avatar May 17 '16 12:05 mgarciadelojo

@mgarciadelojo Thank you for quick answer!

It's will help:)

tarik817 avatar May 17 '16 12:05 tarik817

I am experiencing the same issue with a global scope. This issue has been open since December 31st. Will this ever be fixed or is this package abandoned?

dennis-koster avatar Jul 15 '16 07:07 dennis-koster

I'm acepting pull requests

nicolaslopezj avatar Jul 15 '16 14:07 nicolaslopezj

Any update?

tsunamilx avatar Nov 15 '17 08:11 tsunamilx