Laravel 9 Middleware Tutorial Example
https://www.itsolutionstuff.com/post/laravel-9-middleware-tutorial-exampleexample.html
Last updated
https://www.itsolutionstuff.com/post/laravel-9-middleware-tutorial-exampleexample.html
Last updated
By Hardik Savani June 28, 2022 Category : LaravelPauseUnmuteLoaded: 1.53%Seek to live, currently behind liveLIVERemaining Time -49:29FullscreenCancelHi Dev,
This tutorial will give you example of laravel 9 middleware example. I would like to show you laravel 9 custom middleware example. you'll learn how to create custom middleware in laravel 9. we will help you to give example of how to use middleware in laravel 9.
Laravel Middleware is used to filter HTTP requests in your web application. One of the basic requirements of any web application is an HTTP requests filter, so we have to make one as well for example make auth middleware. auth middleware always checks if you are going then and then you can access those pages.
In this example, we will create one custom middleware that will allow active users to access pages. we will add is_active column in users table and check which user is active or not. so let's follow the below step by step and make it done for example.
Let's follow below steps:
Step 1: Install Laravel
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
Step 2: Create Middleware
In this step, open terminal and run below command to create custom middleware file, so let's run below command:
Now, it's created new IsActive.php file. let's update following code on this file.
app/Http/Middleware/IsActive.php
Read Also: Laravel 9 CRUD Application Tutorial Example
Step 3: Register Middleware
In this file, we need to register middleware on Kernel.php file. we will call is-active of new created middleware. so let's update following file.
app/Http/Kernel.php
Step 4: Use Middleware
In this step, we will create one route and show you how to use middleware in route file. so let's open your route file and update following code:
routes/web.php
Step 5: Create Auth Scaffolding
Here, we will create auth scaffolding so, user can login and check below url. so let's install laravel ui package and generate auth scaffolding.
First you need to install laravel/ui package as like bellow:
Here, we need to generate auth scaffolding in laravel 9 using laravel ui command. so, let's generate it by bellow command:
Now you need to run npm command, otherwise you can not see better layout of login and register page.
Install NPM:
Run NPM:
Step 6: Add is_active Column
Here, we will create new migration to add is_active column in users table. so let's create migration and run it.
Next, simple update below code to migration file.
database/migrations/add_is_active_column.php
Next, we need to run migration using below command:
Now, we will add is_active column in fallible array of user model. let's update it.
app/Models/User.php
Step 7: Create Seeder
In this step, we will create UserSeeder to generate active and inactive users default. so let's create seeder and create dummy users to check.
And put bellow code in UserSeeder seeder this way:
database/seeders/UserSeeder.php
After this we have to run bellow command for run UserSeeder seeder:
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
Now, Go to your web browser, type the given URL and view the app output:
Now you can login with following credentials for InActive User:
After login you have to go on following URL:
Read Also: Laravel 9 Create Multi Language Website Tutorial
You will find following layout:
I hope it can help you...