ProjectsPlaza

Php Projects, Django Projects, Laravel Projects, Codeigniter Projects

Skip to content
  • My account
  • Cart
  • Forums
  • Sell
  • LICENSES
create-custom-admin-login-with-laravel-5

Create custom admin login with laravel 5

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.

admin-login-screen_03


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.

This entry was posted in Laravel, Laravel 5, Php, Programming, Web Development and tagged create application in laravel 5, create custom admin login with laravel 5, create web application in laravel 5, create website in laravel 5, custom admin login with laravel, how to develope website in laravel, how to work with session in laravel, laravel custom admin login, laravel session, laravel session data, laravel turotials, Laravel tutorial series, Learn laravel with example, learn php, save data in session in laravel, working with session in laravel on April 27, 2019 by Projects Plaza.

Post navigation

← Setup laravel 5 on local system and create hello world app Create Read Update and delete data with Laravel 5 →

More posts from this category

  • Create hello world app with fl...
  • Submit Form in Flask
  • Submit form and save data in d...
  • Implement pagination with code...
  • Update delete data from databa...
  • Upload image with codeigniter
  • Learn laravel with example
  • Create Hello World app with la...
  • Create todo app with laravel
  • Create Todo app with laravel &...
  • Upload Image in Laravel 5
  • Generate Dummy data in laravel...
  • Form validation in laravel
  • Add data in database in larave...
  • Create project from scratch wi...
  • Setup laravel 5 on local syste...
  • Create custom admin login with...
  • Create Read Update and delete...
  • Create a hello world app with...
  • Create a single page applicati...

Post Categories

  • Ajax
  • AngularJs
  • APIs
  • Codeigniter
  • Codeigniter Tutorial Series
  • Django
  • Django 3
  • Django Database
  • Django Frontend
  • Express
  • Flask
  • Handlebars
  • Jade
  • Javascript
  • Javascript Frameworks
  • Jquery
  • Laravel
  • Laravel 5
  • Laravel 7
  • Learn python with example
  • Matplotlib
  • Mobile Development
  • MongoDB
  • Mongoose
  • Nodejs
  • npm
  • Php
  • Php Framework
  • Programming
  • Python
  • Python Exercie
  • Python Web Framework
  • React Native
  • ReactJs
  • Uncategorized
  • Web Development
  • WordPress

Post Tags

  • codeigniter 3
  • codeigniter 3 from scratch
  • codeigniter 3 tutorials
  • Codeigniter Example Code
  • Codeigniter Tutorial
  • codeigniter tutorials
  • create project from scratch with django 2
  • django learning blog
  • django tutorials
  • how to create blog app with django
  • laravel 5 tutorials
  • laravel database
  • laravel practise example
  • laravel scripts
  • laravel turotials
  • Laravel tutorial
  • laravel tutorial blog
  • laravel tutorials
  • Laravel tutorial series
  • Learn Codeigniter
  • Learn Codeigniter From scratch
  • Learn Codeigniter with example
  • learn django
  • learn django 2
  • learn django framework
  • learn django from scratch
  • learn django with example
  • learn django with example code
  • learning codeigniter from scratch
  • learning codeigniterphp framework learning
  • learning laravel
  • learning php framework
  • learning php frameworks
  • learning python
  • Learn laravel
  • learn laravel 5
  • Learn laravel from scratch
  • Learn laravel with example
  • learn php
  • learn python 3
  • learn python with example
  • php framework tutorials
  • php scripts
  • projectsplaza
  • web development
Proudly powered by WordPress
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
  • USD $
    USD dollar
  • INR ₹
    Indian Rupee
  • Projects
    • Ideas
    • Django
    • Laravel
    • Codeigniter
    • Core php
    • Bootstrap 4
  • Learn Php
    • Php
    • Laravel
    • Codeigniter
  • Learn Python
    • Python
    • Django
    • Flask
  • Javascript
    • Jquery
    • ReactJs
    • Ajax
    • AngularJs
    • React Native
  • Nodejs
    • Express
    • Jade
    • Handlebars
    • MongoDB