use policy as condition on query builder laravel

126 Views Asked by At

I have this code

    $userSpacesTimelines = $user
                ->spaces()
                ->select('spaces.id', 'spaces.uuid', 'spaces.slug', 'spaces.privacy_type')
                ->with('timeline:id,owner_type,owner_id')
                ->get();

            $filteredSpace = $userSpacesTimelines->filter(function ($space) {
                return auth()->user()->can('viewTimeline', $space);
            });

the bottleneck is on filtering spaces based on viewTimeline policy, and that policy requires lot of tables and 100+ line. so the question is can I use policy as a condition inside my query builder, something like $user->whereCan('viewTimeline')->spaces();

thank in advance for the help

0

There are 0 best solutions below