ivandoric / Making-Websites-With-October-CMS

This is a repository for video tutorial series about making websites with October CMS. You can check out the series here: https://goo.gl/eW32CM
160 stars 52 forks source link

Search on multiple models with sitesearch #24

Open rvgmartins opened 5 years ago

rvgmartins commented 5 years ago

Is it possible to search on several models with different rules?

An example with actors and films is it possible to search on both?

I only find search in one model like this:

$items = Models\Movie::where('name', 'like', "%${query}%") ->orWhere('description', 'like', "%${query}%") ->get();

ivandoric commented 5 years ago

You can do something like this:

$this['videos'] = WatchLearn\Videos\Models\Video::whereHas('series', function ($query) {
        $query->where('slug', '=', $this->param('slug'))->where('published', true);
    })->orderBy('created_at', 'asc')->get();

This is an example from my site so I'm basically getting all the videos that belong to a specific series, and getting that series by slug. So my video is a model, and the Series is a different model. And they of course have a relation.

Hope this helps.