How to add user access control in Laravel (ACL) [Updated 2019]
Hi Coders, Today we are going to add an access control system in our laravel project. If you are building any laravel project then you can follow along and after making some changes things will be working for you. So let’s start
What access control will do
It Will secure your pages, limit user usability based on user role so admin will have maximum access and the user will have minimum access.
So there are many packages available for ACL in laravel. But the one we are going to use is “Laratrust”.You can check it here laratrust .Now let’s begin and follow the steps given below as we going to install this functionality from scratch.
Create a Laravel project if don’t have it already
So starting with the local server. You should have XAMPP installed on your computer. Xampp is a program that provides us server locally so that we can develop our apps using it.
Download and install Xampp from here
Once installed you should get a control panel where you should have “Mysql and Apache” running. It should look like this
Now if all these services are running go and check url=” localhost/phpmyadmin ” you should see something like this
If you see this page that means you have localhost working now we need to proceed to laravel
Open command prompt and select directory “xampp/htdocs”. This should be
In windows C drive where windows are installed
In MacOS it should be in Applications under finder
In linux, it should be “opt/lampp”
Create laravel project by running below given command
composer create-project --prefer-dist laravel/laravel lara
You Should see the creation of a project in command prompt something like this. Here we are creating a “Lara” project.
if things are not working then you might want to install composer for that follow this link and install composer. Once installed re-create project using the above command.
Once installed run this command
If things Worked as expected you should have “lara” project in your “xampp/htdocs” folder.
now we need to install nodejs on our computer.
and install it. In your command prompt/terminal run this command
Prepare to install Laratrust package
Before setting ACL we need to enable login functionality in our project so run below given command
php artisan make:auth
After refresh, you will see login and register link in your project. Its time to create migrate tables provided by laravel.
go to “.env ” file and update database details there.
php artisan migrate
if you get error “fixed-laravel-migration-error-specified-key-was-too-long-max-key-length”
then follow this link to solve it
You should get “user, migration and password-reset table in database”;
Install Laratrust package
Link to the website is here
We need to run a command at this point to install laratrust.
composer require "santigarcor/laratrust:4.0.*"
Then In “config/app.php”.add this under “providers array”:
After this In same “config/app.php”. Add below given line under “aliases”
'Laratrust' => Laratrust\LaratrustFacade::class,
Now run another command in terminal
php artisan vendor:publish --tag="laratrust"
Now We need to run Laratrust migrations so we can create some database tables (laratrust will do this for us).
php artisan laratrust:migration
it will ask confirmation type “Yes” and hit the enter key.
It will add “laratrust_setup_tables.php” under our project “database/migrations”.To run this migration we run
php artisan migrate
Now you will see new tables in the database as given below.
Now we need to create Models files
Run these commands
php artisan make:model Role php artisan make:model Permission
Modify Your Models As Given Below – Include the code that you do not have in your model
Now run the below-given command
Now we have Laratrust installed and we are going to add user roles and permissions in our next post as this post is too long check here