teamtnt / laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
MIT License
1.1k stars 144 forks source link

Searching model with relationship ID constraint #213

Closed chrispage1 closed 3 months ago

chrispage1 commented 5 years ago

Hi,

When I am indexing models, I'm storing both the name and client ID.

[
    'name' => 'Test',
    'client_id' => 3
]

So I'd like to be able to search for my model that has the name test, with the client_id of 3 without even having to hit the database. I've tried the following -

$results = Company
    ::search('test')
    ->where('client_id', 3)
    ->take(10)->get();
$company = new Company;
$company = $company->where('client_id', 3);

$results = Company
    ::search('test')
    ->constrain($company)
    ->take(10)->get();

Now - the great thing is that both the above methods work and return results. The problem I am facing is that I want to check for the presence of the correct client_id property without ever hitting the MySQL database at all. However, every time I run either of the above methods, it doesn't take into consideration the client_id constraint and passes any match for test, regardless of the client_id. Am I doing something wrong, is there a way to achieve this, or is it entirely impossible?

Thanks!