tag
301
4
Laravel

"Can" middleware and route resources?

Posted 8 months ago by tag

I was reading through the documentation for Authorization using Gates and Policies, and while you can do things like this:

Route::put('/post/{post}', function (Post $post) {
        // The current user may update the post...
})->middleware('can:update,post');

You can't seem to do something like this:

Route::resource('post', PostController::class)->middleware('can');

I've tried this doing Gate::resource(...) and registering a policy, but I can't seem to make it automagically apply the gate/policy to the route resource.

Does anyone know of a way to do this or some other mechanism I may be missing?

I'd like to avoid having to do $request->user()->can(....) conditional checking in my controller methods.

Please sign in or create an account to participate in this conversation.