Hello World, Welcome to my website. This is the third part of this series Create project from scratch with laravel 5. In the second part, we have discussed that how to Setup laravel 5 on local system and create hello world app.
In this part, we will learn how we can create custom admin login with laravel 5. In this series, we are building an employee management system. Admin login is an important part of this project because we don’t want to access the employee panel without authority. This part helps us to authorize the admin who is accessing this panel.
I have divided this tutorial into the following parts.
- Create Admin Controller
- Create Login View
- Setup route for accessing the login form via the admin controller
- Create Admin model and migration
- Perform Admin Login and save data in session in laravel and redirect to the admin dashboard
Create Admin Controller
- Go to the command line and enter in the project folder.
- Run the following artisan command to create the admin controller
-
> php artisan make:controller AdminController
Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.
- After running the above command, you will see that a new file has been created in the app/Http/Controllers folder. This is our Admin Controller file.
Create Login View
- Create login.blade.php in resources/views folder.
- Add the following code in this file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> {{-- Bootstrap 4 --}} <link rel="stylesheet" href="{{ asset('lib/bootstrap.min.css') }}"> <title>Admin Login</title> </head> <body> <div class="container"> <div class="row mt-5"> <div class="col-sm-6 offset-sm-3"> <div class="card"> <div class="card-header"> Admin Login </div> <div class="card-body"> <form method="post" action=""> <table class="table table-bordered"> <tr> <th>Email</th> <td><input type="text" class="form-control" name="email"></td> </tr> <tr> <th>Password</th> <td><input type="password" class="form-control" name="password"></td> </tr> <tr> <td colspan="2"> <input type="submit" class="btn btn-primary" value="Login" /> </td> </tr> </table> </form> </div> </div> </div> </div> </div> </body> </html>
In the above file, I have created a login form with bootstrap 4.
Setup route for accessing the login form via the admin controller
- Open routes/web.php file and the following code at the end.
Route::get('admin/login','AdminController@login');
- Open app/Http/Controllers/AdminController.php file and add the following code the AdminController Class.
// function login(){ return view('login'); }
- Now access the project URL followed by /admin/login. You will see the following screenshot.
Create Admin Model
- Go to the command and enter in this project.
- Run the following artisan command to create Admin Model.
> php artisan make:model Admin -m
- The above command will create Admin Model in app folder and migration file in database/migrations folder.
- Open newly created migration file and replace the up function code with following code.
Schema::create('admins', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('admin'); $table->string('password'); $table->timestamps(); });
Above code will create a admins table with defined fields. Before doing this, we have to define database in .env file. I am using xamp so i will add the following credentials.
- host=localhost
- user=root
- password=’ ‘
Now te following command in command line
> php artisan migrate
Above command will create the table in your defined database.
Perform admin login and save data in session the redirect to admin dashboard
We have created the controller, view and model admin login. Now we will perform admin login. In this section, we will edit the controller, view and route files.
- First fo all add action in form tag in login as following
<form method="post" action="{{ url('admin/login') }}"> {{ csrf_field() }}
- Add the following code in routes/web.php file
Route::post('admin/login','AdminController@submit_login'); Route::get('admin/dashboard','AdminController@dashboard');
- Add the following methods in the AdminController after login method
// Submit Login function submit_login(Request $request){ $request->validate([ 'email'=>'required|email', 'password'=>'required' ]); $admin=Admin::where(['admin'=>$request->email,'password'=>$request->password])->count(); if($admin>0){ session(['adminSession'=>true]); } return redirect('admin/dashboard'); } // Admin Dashboard function dashboard(){ return view('dashboard'); }
- Create dashboard.blade.php file in resources/views folder.
- Add he following code above the form tag in view file.
@if($errors->any()) @foreach($errors->all() as $e) <p class="text-danger">{{ $e }}</p> @endforeach @endif
Above code will print the form error message if you leave it empty.
- Add the custom data in admins table for admin login attempt. For example, in the admin column add admin@admin.com and in the password column add admin.
- After Successfull login attempt, you will redirect in admin dashboard.
In the next part of this series, We will design admin dashboard create Employee CRUD for adding, updating, deleting and reading data. if you have any query then please send in the comment section.