Search results based on conditional relationship

Viewed 215

I am using Laravel 5.5 and the TNTSearch engine for Laravel Scout. The issue I have stumbled upon is to do with Eloquent relationships and whether I can do conditional checks for them. Laravel Scout includes a where function like Eloquent, but it is a dumbed down version of it and only allows column checks and nothing else.

Search Controller

protected function search()
{
    $query = request()->input('query');

    $meta = $this->searchMeta($query, 10);
    $items = $this->searchItems($query, 10);

    return view('model.search.index')->with([
        'meta' => $meta,
        'items' => $items
    ]);
}

protected function searchMeta($query, $limit = 100)
{
    return ItemMeta::search($query, function ($tntsearch, $query, $options) use ($limit) {

        return $tntsearch->search($query, $limit);
    })->where('company_id', \Auth::user()->company_id)->get();
}

protected function searchItems($query, $limit = 100)
{
    return Item::search($query, function ($tntsearch, $query, $options) use ($limit) {
        return $tntsearch->search($query, $limit);
    })->get(); // Need to be restricted to company
}

The goal of what I want to do is check whether the item is related to the current user's company. I do that by having a company_id column on the Meta table, the Item then has a column called "meta_id" which is a foreign key to Meta.

How would I go about only showing results for the current company, when the item uses a relationship to check whether a user has access or not? If it is not possible with Laravel Scout or the TNTSearch engine, does anyone know of alternatives that would allow me to do this?

0 Answers
Related