Ad

Show Pop Up Only First Time User Login (Laravel)

- 1 answer

I have a simple question about a project I working on it I need to show a Modal Popup once when the user login in first time no more only once!!! I create this code but it still not working

test.blade.php

@if ($first_time_login)
   <h3>Welcome Popup</h3>
@else 
  <h3>Hey! 🖐 Nothing to Show</h3>
@endif

TestController

public function Test()
{

    if (Auth::user()->first_time_login) {
        $first_time_login = true;
        Auth::user()->first_time_login = 1;
        Auth::user()->save();
    } else {
        $first_time_login = false;
    }

    return view(
        'test', 
        ['first_time_login' => $first_time_login]
    ); 
}

2014_10_12_000000_create_users_table.php

    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name')->nullable();
        $table->string('email')->unique()->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password')->nullable();
        $table->rememberToken()->nullable();
        $table->timestamps();
        $table->string('first_time_login')->default(false);
    });
Ad

Answer

I see a few things on your code.

Firstly you are declaring the first_time_login field as a string, it should be boolean with default value of true. Like this:

2014_10_12_000000_create_users_table.php

    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name')->nullable();
        $table->string('email')->unique()->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password')->nullable();
        $table->rememberToken()->nullable();
        $table->timestamps();
        $table->boolean('first_time_login')->default(true);
    });

Another thing, after checking if it's a first time login you are setting it to 1. This will make your field remain as true. Change it to:

TestController

public function Test()
{

    if (Auth::user()->first_time_login) {
        $first_time_login = true;
        Auth::user()->first_time_login = false;
        Auth::user()->save();
    } else {
        $first_time_login = false;
    }

    return view(
        'test', 
        ['first_time_login' => $first_time_login]
    ); 
}

That should do it.

Ad
source: stackoverflow.com
Ad