When a project is added, I fetch all users who have the permission to view it to inform them.
This happens in a listener:
use App\Models\User;
use Illuminate\Support\Facades\Gate;
// ...
$notifiables = User::all()->filter(function(User $user) use($event) {
return Gate::forUser($user)->allows('view', $event->project);
});
This works. But when I've got 1000 users in my system and 5 are able to view the project, I'm iterating over 995 users for nothing.
And as the event and the listener run synchronously, the user adding a project has to wait for this to happen.
How can I accelerate this?
Edit: Okay, it's possible to queue listeners. However, it would be great to improve this code.