nicolaslopezj / searchable

A php trait to search laravel models
MIT License
2.01k stars 291 forks source link

Scoping to current tenant when searching through pivot table columns #159

Open lasseeee opened 6 years ago

lasseeee commented 6 years ago

Is there a way to limit what rows are being returned when searching pivot data on a many-to-many relation? I have users show up because they match pivot data on the many-to-many table which belongs to other tenants. I'm trying something along the lines of, without any luck.

User::search($q)
    ->whereHas('tenants', function ($query) use ($tenant) {
    $query->where('tenant_id', $tenant->id);
})->get();