CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE Videos New

Login Signup with Laravel


Laravel provides artisan command to create register and login feature in project.

Run php artisan make:auth in terminal.

php artisan make:auth

The above command create all related files and routes.

You may customize the user table. Open migration file and Add/change the field and according to that change in RegisterController.php and resources/views/auth/register.blade.php files.

Run php artisan migrate in terminal.

php artisan migrate

Now Register login feature is integrated in your project.

Note: If you are getting migration error then reduce the size of email.Open migration files and put
second parameter in $table->string('email', 191)->unique(); and $table->string('email', 191)->index();
and then try.

If you want customization( Adding username)

  1. Run php artisan make:auth in terminal.
  2. Open create_users_table.php migration file and add the following line
    $table->string('username',191)->nullable()->unique();
  3. Run php artisan migrate in terminal
  4. Now open RegisterController.php and add following line.
protected function validator(array $data)
{
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'username' => 'required|string|max:191|unique:users',
            'email' => 'required|string|email|max:191|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
}
protected function create(array $data)
{
    return User::create([
            'name' => $data['name'],
            'username' => $data['username'],
            'email' => $data['email'],
    'password' => bcrypt($data['password']),
    ]);
}

save this file.

  • Open User.php model and add the following code and save it
  • protected $fillable = [
        'name', 'email', 'password','username',
    ];
        
  • Open resources/views/auth/register.blade.php file , add following code just below the name field and save it
  • <div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
     <label for="username" class="col-md-4 control-label">Username</label>
     <div class="col-md-6">
       <input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required>
            @if ($errors->has('username'))
                <span class="help-block">
                    <strong>{{ $errors->first('username') }}</strong>
                </span>
            @endif
        </div>
    </div>
        
  • Open LoginController.php file add following code and save it.
  •     import it above the class 
    use Illuminate\Http\Request;
    add the code just below the constructor function
    
    protected function credentials(Request $request) 
    {
        
        /// this method is overriden form Illuminate\Foundation\Auth\AuthenticatesUsers; class
        $field = filter_var($request->get($this->username()), FILTER_VALIDATE_EMAIL)
            ? $this->username()
            : 'username';
    
        return [
            $field => $request->get($this->username()),
            'password' => $request->password,
        ];
    }
         
  • Open login.blade.php file add the following code and save it
  • Only change the type of Email field from email to text and change label 
    <label for="email" class="col-md-4 control-label">E-Mail/Username</label>
    <input id="email" type="text" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
         
  • pages you want to access only after login then add the following code in respective controller files
  •         public function __construct()
        {
            $this->middleware('auth');
        }
         
  • If you want to set custom session add following code
  •      protected function authenticated(Request $request, $user)
    {
        // overriden from Illuminate\Foundation\Auth\AuthenticatesUsers; class
        
        //set session
        $request->session()->put('admin', 'admin');
        //redirect
       return redirect()->intended($this->redirectPath());
    }