Drawing a blank here as I have been out of the game for some time and busy on a new application.
Objective: Send a newly registered user a welcome message with a modal when he logs in for the first time.
This should only happen the first time he logs in, for example:
newuser.blade.php
@if ($first_login == true)
<h3>Welcome Popup</h3>
@else
<h3>Hey! 🖐 Nothing to Show</h3>
@endif
I have added a boolean column to my users
table called: first_login
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('first_login')->default(true);
});
}
Made a controller called FirstLogin.php
:
<?php
namespace AppHttpControllers;
use Auth;
use IlluminateHttpRequest;
class FirstLogin extends Controller
{
public function FirstLogin()
{
if (Auth::user()->first_login) {
$first_login = true;
Auth::user()->first_login = true;
Auth::user()->save();
} else {
$first_login = false;
}
return view(
'test',
['first_login' => $first_login]
);
}
}
What are now the next steps? I am really pulling a blank on this as, like I said, been years not building anything.
So to cut it short, the column should update after the user logs out I guess?
Because if we update the column when the user logs in the first time the modal can not be shown.
I have the feeling I am forgetting something here…
Any advice is appreciated.
3
Answers
You can modify the login and logout methods in the relevant controller, e.g., LoginController, or wherever you handle the user login and logout process. You can update the
first_login
column.You can store a variable named
firstLogin
in the session when the use login for the first time.You might use the following controller to have the sessions flag created.
Then in your blade template you can check if the session variable exists:
Note: This will keep showing the popup to user until a new session is established.
You can rename the
first_login
column tologged_in_before
with default value false:And then, change it to true on the first opening of your dashboard:
Additionally, you can not bind the logic to first login and just make variable
is_welcome_modal_shown
that will work as well as in the code above. Anyway, it guarantees that the modal will be shown one time and doesn’t require any tricks with session and logout.