Hello Developes in this tutorial we will discuss about how you can make any table or any model authenticable using Auth::gaurd() .
|1||What is Auth::gaurd()|
|2||Creating Model & migration|
|3||Define model as authenticable|
|4||Define model in config/auth.php|
|5||Setting up login form|
|6||Authenticate with auth::guard|
Auth::gaurd() is a beautiful feature based on OOPS provided by laravel which provide us to make any database table or any model autheticable property similar to User model , for eg working on big projects like multi-vendor E-Commerce where we have different roles of a user (admin , seller , customer) and different table as well , so there to provide autheticable property to seller similar to our user model Laravel provide us Auth::gaurd().
We are considering an example of Seller user as table in database , so firstly we start by creating model and migration of Seller.
Code for model:Copy
This will create a model app\Models\Seller.php and a migration file in database/migrations , now we will migrate our migration and then we setup our Seller model
code for migration : Copy
Now run the migration
After migration we will move towards our model to provide same properties as the User model in app/Models/Seller.php model , so in seller model we are providing Authenticatable property and Notifiable is optional to add.
code for Seller model :Copy
Now here is the main code comes where we define our Seller model as auth::guard() in config/auth.php , so basically in this auth.php we provide all our authenticable models or database table here.
code for config.auth.php: Copy
For checking our user authentication we will check with simple login form
code for web.php Copy
Also read: JOBS IN LARAVEL 10 , JOBS-QUEUES IN LARAVEL 10.
code for chekUser.blade.php Copy
code for controller Copy
As shown in Controller we are first creating our User and Seller with createUser() function and then we check if user exist in users table or sellers table.
To autheticate Seller Auth::guard('seller')->attempt($credentials, false) whcih validate the credentials that we have send to the controller function and to check if Seller user if login or autheticate we simply use
Auth::guard code :Copy
0 Comments (Please let us know your query)