SiteSearch

Adds global search capabilities to October CMS

Back to SiteSearch Support

alava
alava

Hi, I would like to search not only by query but also by another variable. So I have query and status. I can add it in the search input and generate right url (like ?q=sometning&status=3). It returns right query but all statuses. Can you help me?

Code in my plugin.php:

public function boot() { \Event::listen('offline.sitesearch.query', function ($query) {

        // The controller is used to generate page URLs.
        $controller = \Cms\Classes\Controller::getController() ?? new \Cms\Classes\Controller();

        if (isset($status)) {
            // Search your plugin's contents
            $items = \Alava\NameOfPlugin\Models\Item
            ::where('title', 'like', "%${query}%")
            ->orWhere('description', 'like', "%${query}%")
            ->where('status', 'like', "%${status}%")
            ->get();
        } else {
            // Search your plugin's contents
            $items = \Alava\NameOfPlugin\Models\Item
            ::where('title', 'like', "%${query}%")
            ->orWhere('description', 'like', "%${query}%")
            ->get();
        }; ....

Last updated

OFFLINE
OFFLINE

You need to make sure your query is nested corretly since it has an orWhere in it:

$items = \Alava\NameOfPlugin\Models\Item
            ::where(function($q) {
                 $q->where('title', 'like', "%${query}%")
                   ->orWhere('description', 'like', "%${query}%")
            })
            ->where('status', 'like', "%${status}%")
            ->get();
alava
alava

Thank you so much, but there is another problem, because now I get error Undefined variable: status

I added $status to ResultsProvider.php, Result.php, ResultCollection.php, SearchService.php in your plugin's classes and to SearchResults.php in components. dd($status) works right in every of these files

But function in my plugin.php gives me error Undefined variable: status

Any idea why?

Last updated

1-3 of 3