How to check if this is a visitors first visit Laravel Updated

  controller, laravel, middleware, php

I am making a test crypto wallet and I would like my index to redirect to the key setup if this is their first visit currently I have sessions database set up with a boolean for the first visit with a default value of true that is changed to false when the page loads I have tried using middleware one that runs before loading as below:

public function handle(Request $request, Closure $next){
(session('first_visit') ? session(['first_visit => 'false']) : session(['first_visit' => 
return $next($request);

and one that loads after the page does that’s set up like this:

public function handle(Request $request, Closure $next){
$responce = $next($request);
session(['first_visit' => 'false']);
return $responce;

in my controller, I’ve done this:

public function first_visit(Request $request){
    return view('welcome', ['first_visit' => session('first_visit')]);

I installed debugbar and I see this array under requests:

array:5 [▼
"first_visit" => "false"
"_token" => "9rKGFbTm5WeSYJFBw25L8DQZ6ofb8I9grdWg48BZ"
"_previous" => array:1 [▶]
"_flash" => array:2 [▶]


inside my welcome.blade.php file I have @if($first_visit) then the code for the first visit page with @else for the usual homepage code I am always shown what’s inside the if statement unless I put @if(!$first_visit) there’s no requests listed to set first_visit from the page what am I doing wrong?

Source: Laravel

Leave a Reply