Many past answers on this.
site:laracasts.com your search sentence
Also
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have noticed that each controller has a construct method. I have separated my code functionality into individual controllers (e.g. run admin functionality, run query functionality). i would like to restrict access to the controllers for each role of the user. i can easily check to see if the user is part of the roll via a db call. can i do the check in the construct of the controller, or is there a better place to do this?
You said you want to restrict access to the controllers for different uses.
It is better if you use routing along with query scopes to fine-tune who can see what.
Example look at the links I gave and look at this link.
https://laracasts.com/discuss/channels/laravel/using-laravel-policy-to-filter-eloquent-query
Because sometimes a user can see their own data yet an admin can see anyone's data. So restricting a whole controller it's not really a good idea.
The only time this is a good idea is take a hospital where Physicians have separate logins all together than patients.
As said I am now using scopes, like:
public function scopegetPets($query, $petsearch = '')
{
$petsearch = $petsearch . "%";
$query->where('petname', 'like', $petsearch);
if (ChkAuth::userRole('admin') === false) { // I have custom RBAC helpers
$userid = Auth::user()->id;
$query->where('ownerid', '=', $userid);
}
$results = $query->orderBy('petname', 'asc')->paginate(5);
return $results;
}
But all RBAC has the same purpose, a logged in user either can or cannot do something. Or a user can see and edit their own data, not someone else's. That's where the Auth::user()->id comes in.
A spatie example:
public function update(Request $request, Post $post) {
if ($post->author !== auth()->user()->id || auth()->user()->cannot('edit posts'))
abort(404);// or some other
}
}
So no matter if you use laravel's authorization, spatie, or custom rbac, the goal is the same.
I use the out of box authentication, but have custom helpers to work with that and roles I setup.
But until you know php and frameworks well, I would not suggest attempting your own rbac.
I would also suggest writing all out with pencil and paper first.
Even Jeffrey in a video mentions, it is tricky at first til you begin learning it. Security is something to take your time and learn correctly.
I would also suggest taking a good general RBAC tutorial, so later you will know how to write and use it.
Namely, a system that can be used in laravel, cakephp, or yii2. Or even another language if needed, with just small tweaks.
I try to not write everything "framework" biased.
Please or to participate in this conversation.