Laravel 7 Livewire Login Registration Example Tutorial
In this article, we would like to introduce you to how to create a Login Registration Example using Livewire in laravel 7.
So you can create the custom login register page using the below steps.
Overview
Step 1: Install Laravel 7
Step 2: Setting Database Configuration
Step 3: Run Migration Command
Step 4: Install Livewire
Step 5: Create Component
Step 6: Create Route
Step 7: Create Blade Files
Step 8: Run Our Laravel Application
Laravel Livewire Login Register Example Tutorial
Step 1: Install Laravel 7
We are going to install laravel 7, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.
1 | composer create-project --prefer-dist laravel/laravel laravel7_livewire_login_register |
Step 2: Setting Database Configuration
After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
1 2 3 4 5 6 | DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(laravel7_livewire_login_register) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root) |
Step 3: Run Migration Command
Run the below command for migratioin of users table.
1 | php artisan migrate |
Step 4: Install Livewire
Now, We will install a livewire package using the below command.
1 | composer require livewire/livewire |
Step 5: Create Component
Here below command help to create the component.
1 | php artisan make:livewire LoginRegister |
app/Http/Livewire/LoginRegister.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?php namespace App\Http\Livewire; use Livewire\Component; use Hash; use App\User; class LoginRegister extends Component { public $users, $email, $password, $name; public $registerForm = false; public function render() { return view('livewire.login-register'); } private function resetInputFields(){ $this->name = ''; $this->email = ''; $this->password = ''; } public function login() { $validatedDate = $this->validate([ 'email' => 'required|email', 'password' => 'required', ]); if(\Auth::attempt(array('email' => $this->email, 'password' => $this->password))){ session()->flash('message', "You are Login successful."); }else{ session()->flash('error', 'email and password are wrong.'); } } public function register() { $this->registerForm = !$this->registerForm; } public function registerStore() { $validatedDate = $this->validate([ 'name' => 'required', 'email' => 'required|email', 'password' => 'required', ]); $this->password = Hash::make($this->password); User::create(['name' => $this->name, 'email' => $this->email,'password' => $this->password]); session()->flash('message', 'Your register successfully Go to the login page.'); $this->resetInputFields(); } } ?> |
Step 6: Create Route
Add the following route code in the “routes/web.php” file.
1 | Route::view('login','livewire.home'); |
Step 7: Create Blade Files
So finally, first we will open the home.blade.php and login-register.blade.php files and after then paste the following code.
resources/views/livewire/home.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!DOCTYPE html> <html> <head> <title>Laravel 7 Livewire Login Registration Example Tutorial - XpertPhp</title> <script src="{{ asset('js/app.js') }}" defer></script> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> @livewireStyles </head> <body> <div class="container"> <div class="row mt-5 justify-content-center"> <div class="mt-5 col-md-8"> <div class="card"> <div class="card-header bg-danger text-white"><h5 style="font-size: 23px;">Laravel Livewire - Login Register Example - NiceSnippets.com</h5></div> <div class="card-body"> @livewire('login-register') </div> </div> </div> </div> </div> @livewireScripts </script> </body> </html> |
resources/views/livewire/login-register.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <div> <div class="row"> <div class="col-md-12"> @if (session()->has('message')) <div class="alert alert-success"> {{ session('message') }} </div> @endif @if (session()->has('error')) <div class="alert alert-danger"> {{ session('error') }} </div> @endif </div> </div> @if($registerForm) <form> <div class="row"> <div class="col-md-12"> <div class="form-group"> <label>Name :</label> <input type="text" wire:model="name" class="form-control"> @error('name') <span class="text-danger error">{{ $message }}</span>@enderror </div> </div> <div class="col-md-12"> <div class="form-group"> <label>Email :</label> <input type="text" wire:model="email" class="form-control"> @error('email') <span class="text-danger error">{{ $message }}</span>@enderror </div> </div> <div class="col-md-12"> <div class="form-group"> <label>Password :</label> <input type="password" wire:model="password" class="form-control"> @error('password') <span class="text-danger error">{{ $message }}</span>@enderror </div> </div> <div class="col-md-12 text-center"> <button class="btn text-white btn-success" wire:click.prevent="registerStore">Register</button> </div> <div class="col-md-12"> <a class="text-primary" wire:click.prevent="register"><strong>Login</strong></a> </div> </div> </form> @else <form> <div class="row"> <div class="col-md-12"> <div class="form-group"> <label>Email :</label> <input type="text" wire:model="email" class="form-control"> @error('email') <span class="text-danger error">{{ $message }}</span>@enderror </div> </div> <div class="col-md-12"> <div class="form-group"> <label>Password :</label> <input type="password" wire:model="password" class="form-control"> @error('password') <span class="text-danger error">{{ $message }}</span>@enderror </div> </div> <div class="col-md-12 text-center"> <button class="btn text-white btn-success" wire:click.prevent="login">Login</button> </div> <div class="col-md-12"> Don't have account? <a class="btn btn-primary text-white" wire:click.prevent="register"><strong>Register Here</strong></a> </div> </div> </form> @endif </div> |
Step 8: Run Our Laravel Application
We can start the server and run this example using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/login |
Read Also
Laravel 6 CRUD (Create Read Update Delete) Tutorial For Beginners
Laravel 7 CRUD Operation With Ajax Example
Laravel 6 CRUD Operation With Ajax Example
Laravel 6 Pagination Example Tutorial
Laravel 7 Pagination Example Tutorial