Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Implement Remember Me Feature in Laravel

How to Implement Remember Me Feature in Laravel ?

Aman Jain, August 12, 2022March 16, 2024

Laravel authentication offers remember me functionality out of the box and to implement remember me feature in laravel we only need to follow some guidelines provided by laravel so in this article i will show you the simplest way to implement this. Most of the time while developing login functionality of application its required that to handle the remember me checkbox so that client do not need to keep login again and again on visiting the website back.

We will take a simple example of login form and submitting of it to laravel authentication. we will create a checkbox whether user want to remember the login or not and input box for email and password.

this example will work in any version of laravel 5, laravel 6, laravel 7, laravel 8, and laravel 9. So let’s begin the tutorial.

So there is 2 most important thing we need to do is as follow

Step 1 : Add remember_token column in your users table

In this step you need to add remember_token in your users table so we can store the token on click of remember me checkbox so check your table first if its exist in users table then okay otherwise you need to create it as follow

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Step 2 : Use Auth::attempt() method and Pass second argument as true

Now, use Auth::attempt() and pass the second argument true to enable the remember me functionality as follow

public function authnticate(Request $req)
{
    $req->validate([
        'email' => 'required|email|min:4|max:50',
        'password' => 'required'
    ]);

    $remember_me = $req->has('remember_me') ? true : false;

    $check = $req->only('email', 'password');
    
    if(\Auth::attempt($check, $remember_me))
    {
        return redirect('user/dashboard');
    }
    else
    {
        session()->put('error-msg', "Please enter the valid username and password");
        return redirect('user/login');
    }
}

Learn Also : Laravel 8 Custom login and registration with example or Laravel ajax login and registration with example

Related

Php Laravel Laravel 9 laravelloginremember me

Post navigation

Previous post
Next post

Related Posts

Php Laravel Rest Api OTP Login and Registration Without Password

Laravel Rest Api OTP Login and Registration Without Password

September 3, 2022March 16, 2024

Today most of the applications are using laravel rest api otp login and registration without password for ease of login and registration and also provides easy to login without remembering the password each time while login or register. In Laravel there is multiple ways to implement the login and registration…

Read More
Php How to Get Last Record With Group By in Laravel Eloquent

How to Get Last Record With Group By in Laravel Eloquent ?

September 7, 2022March 16, 2024

In this tutorial we will learn to get last record with group by in laravel eloquent. while working with group by in mysql its gives first record from the group by records that means if you have multiple record with same id and you want the most recent record or…

Read More
Php Target class controller does not exist in Laravel

Target class controller does not exist in laravel 8 or Laravel 9

August 20, 2022March 16, 2024

Target class controller does not exist in laravel 8 or laravel 9 error encounters in laravel versions above 8 because Before laravel 8 we can call controller without full namespace but after laravel 8, laravel team has changed the way to call the controller class. In laravel 5 and 6…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version