Laravel 7/6 Auth Login with Username or Email Tutorial
In this post we will give you information about Laravel 7/6 Auth Login with Username or Email Tutorial. Hear we will give you detail about Laravel 7/6 Auth Login with Username or Email TutorialAnd how to use it also give you demo for it if it is necessary.
Let’s start with how to login with username or email in laravel 7/6 auth. i will give you simple solution of laravel 7/6 login with username or email in authentication. it’s easy to make auth login with username and email address in laravel 7/6 application.
Sometime, we need to create login page with username or email address to login. it’s great function if you have in your website because it’s really easy to remember anyone to your customer. customer if forgot email then he has one username. So it’s really helps.
In laravel 6, i will give how you can setup for login with username or email step by step. so let’s follow bellow steps.
Step 1: Install Laravel 6
first of all we need to get fresh Laravel 6 version application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install Laravel UI
You have to follow few step to make auth in your laravel 6 application.
First you need to install laravel/ui package as like bellow:
composer require laravel/ui
Step 3: Generate Auth Scaffold
Here, we need to generate auth scaffolding in laravel 6 using laravel ui command. so, let’s generate it by bellow command:
php artisan ui bootstrap --auth
Now you need to run npm command, otherwise you can not see better layout of login and register page.
Install NPM:
npm install
Run NPM:
npm run dev
Step 4: Add Username Column
Now add new column username to your users table, so you can simply update your migration as like bellow.
migration
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email');
$table->string('username')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->boolean('is_admin')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Now you can run migration
php artisan migrate
Step 5: Update Login View
Laravel has created default login blade file. we need to add comman username field on it and remove email field. so let’s update like as bellow:
resources/views/auth/login.blade.php
....
<div >
<label for="username" >Username Or Email</label>
<div >
<input id="username" type="username" name="username" value="{{ old('username') }}" required autofocus>
@error('username')
<span role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
....
Step 6: Overwrite Login method
Now we have to overwrite login with on LoginController file. so let’s add logincontroller like as bellow:
app/Http/Controllers/Auth/LoginController.php
<?php
namespace AppHttpControllersAuth;
use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;
use IlluminateHttpRequest;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Create a new controller instance.
*
* @return void
*/
public function login(Request $request)
{
$input = $request->all();
$this->validate($request, [
'username' => 'required',
'password' => 'required',
]);
$fieldType = filter_var($request->username, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
if(auth()->attempt(array($fieldType => $input['username'], 'password' => $input['password'])))
{
return redirect()->route('home');
}else{
return redirect()->route('login')
->with('error','Email-Address And Password Are Wrong.');
}
}
}
You can add some dummy records on your users table.
Now you can run your project.
You will get layout of login page like as bellow:
I hope it can help you…
Hope this code and post will helped you for implement Laravel 7/6 Auth Login with Username or Email Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs