I'm having a trouble with creating the "owner" middleware.
For example, I have a Articles
and User
model associated with user_id
key.
I want to add the "owner" middleware to the ArticlesController
, so the only owner of that article can edit, update and delete it.
I've been searching for this issue for a while, but never found the code, which would work. Some of them tried to make it work with Form Requests, but I'm interested in using Middleware.
Alternatively you could use route and middleware parameters, it has some advantages:
Here’s the middleware (
app/Http/Middleware/AbortIfNotOwner.php
):Inside
app\Http\Kernel.php
:Inside your route file (
app/Http/routes.php
):And optionally call it in the controller:
app\Http\Kernel.php
: