While trying to filter an Eloquent query by a pivot column I'm facing an issue with using wherePivot inside a where function.
The following syntax doesn't work:
use Illuminate\Database\Eloquent\Builder;
$team->users()
->when($report === 'status', fn (Builder $users) => $users->wherePivot('has_status_reporting', true))
->get();
The following syntax works:
use Illuminate\Database\Eloquent\Builder;
$team->users()
->wherePivot('has_status_reporting', true)
->get();
I've found the following GitHub issue... but it seemingly was just closed without a solution by the Laravel team: https://github.com/laravel/framework/issues/38728
Is there any solution to this? I guess I might as well just put the query in a variable and add the wherePivot depending on the $status variable "oldschool".
Kind regards!