Home

Hi There,

I’m Andrae, a Software Developer. I love sharing things that I have learned, and things that work efficiently. I’ve shared some programming posts here. Hopefully you find it helpful. Free free to leave a comment.


Check out my most recent blog post:

Laravel 5.5 permissions based on user id

For a Laravel 5.5 project I solved a permission problem that i’d like to share in case someone else can benefit.

Problem:

The structure of our urls look like this:

users/5/companies

Which will show all the companies for user 5. I however wanted to limit it so that only user 5 may see user/5 company listings. User/4 or another use should see access denied when viewing /users/5/compaines.

My current route was defined this way in routes/web.php:

Solution:

Use middleware attached to a route group with prefix to do the check. Although you can accomplish this in a controller, you are better off using middleware. Middleware intercepts the request before it even its your controllers. So you can stop it at an earlier stage. (more…)