Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alizey's avatar

How to show successful message for one time only

I want to show a success message when users login

return redirect('/dashboard')->with('success', 'You are successfully logged in'); 

And at dashboard i am doing this

@if(Session::has('success'))
<div class="alert alert-warning alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{Session::get('success')}
</div>
@endif

All things works fine and i got the message to.But when ever i change any page and goes back to home page i got the message again of successfull login.

When ever i clicks on dashborad. How can i restrict that message for one time only when user login.

0 likes
17 replies
jekinney's avatar

Your hard coding the success data to the route. Does it happen when you hit the back button or go to the homepage directly?

Suggest looking at Laracasts flash package too. Super easy to use and it will eliminate your issue.

Alizey's avatar

when evers i goes to homepage i got the message.and i am also using the Flash package. I try both but same result @jekinney

umefarooq's avatar

@Alizey if you are always checking user login and redirecting then this session always set and you will get this message

Alizey's avatar

No @umefarooq its for just one time when user login. But on every refresh i got the message again and agian

Alizey's avatar

i want's same thing when we Signin to Laracasts.com its show us you are login for just one time until we logout

frezno's avatar

@Alizey

why don't you watch the video i mentioned above. It explains exactly what you are looking for.

1 like
Alizey's avatar

@frezno i watched that many times and i am using notifications on my whole project and they works fine on edit delete or any operation even at logout. Without any problem. The only problems occurs at login. when user login when he reload the home page login session message apears on every refresh.

pmall's avatar

The only problems occurs at login. when user login when he reload the home page login session message apears on every refresh.

Do you redirect the user after he gets logged in ? Show the controller code please.

Alizey's avatar

@pmall

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\RedirectResponse;
use Laracasts\Flash\Flash;
use Auth;

class RedirectIfAuthenticated {

    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->check())
        {
            $name = Auth::user()->name;
            //return new RedirectResponse(url('/dashboard'));
                    Flash::success("You are successfullt login! $name");
            return redirect('/dashboard'); 
        }

        return $next($request);
    }

}

Here it is. And @ my view using @include('flash::message') this. Notificatons shows when user login. Let suppose i click on about page its simply load the about page. but when i click on home page it show me again the notification . that you are loged in

pmall's avatar

Show your routes and the controller handling the login. You should put the flash message in the controller action, not in a middleware.

Alizey's avatar

Here it is. @pmall

<?php namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Laracasts\Flash\Flash;

trait AuthenticatesAndRegistersUsers {

    /**
     * The Guard implementation.
     *
     * @var \Illuminate\Contracts\Auth\Guard
     */
    protected $auth;

    /**
     * The registrar implementation.
     *
     * @var \Illuminate\Contracts\Auth\Registrar
     */
    protected $registrar;

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function getRegister()
    {
        return view('auth.register');
    }

    /**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postRegister(Request $request)
    {
        $validator = $this->registrar->validator($request->all());

        if ($validator->fails())
        {
            $this->throwValidationException(
                $request, $validator
            );
        }

        $this->auth->login($this->registrar->create($request->all()));

        return redirect($this->redirectPath());
    }

    /**
     * Show the application login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function getLogin()
    {
        return view('auth.login');
    }

    /**
     * Handle a login request to the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email', 'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');

        if ($this->auth->attempt($credentials, $request->has('remember')))
        {
            return redirect()->intended($this->redirectPath());
        }

        return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors([
                        'email' => $this->getFailedLoginMessage(),
                    ]);
    }

    /**
     * Get the failed login message.
     *
     * @return string
     */
    protected function getFailedLoginMessage()
    {
        return 'These credentials do not match our records.';
    }

    /**
     * Log the user out of the application.
     *
     * @return \Illuminate\Http\Response
     */
    public function getLogout()
    {
        $this->auth->logout();
        
        Flash::success("You have been logged out.");
        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
    }

    /**
     * Get the post register / login redirect path.
     *
     * @return string
     */
    public function redirectPath()
    {
        if (property_exists($this, 'redirectPath'))
        {
            return $this->redirectPath;
        }

        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/';
    }

    /**
     * Get the path to the login route.
     *
     * @return string
     */
    public function loginPath()
    {
        return property_exists($this, 'loginPath') ? $this->loginPath : '/auth/login';
    }

}

pmall's avatar
pmall
Best Answer
Level 56

This middleware is made to redirect an user somewhere when he is signed in and trying to access a page only accessible to guest user (like the login form). If the user is logged in, redirect somewhere. Obviously not the place to put your flash message.

You have to put the flash message in the postLogin method.

Alizey's avatar

Routes

Route::get('/', array('as' => '/', 'uses' => 'Auth\AuthController@getLogin'));
Route::post('/', array('as' => '/', 'uses' => 'Auth\AuthController@postLogin'));

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

Alizey's avatar

@pmall Thanks you are right :D i just placed my Message @postLogin Function and its works perfectly :D Thanks for that usefull info

ayoob's avatar

Just Create An Event for example . Here I am using this lib roksta/toast

class CreateUser
{
    /**
     * The user instance.
     *
     * @var \Illuminate\Contracts\Auth\Authenticatable
     */
    public $user;

    /**
     * Create a new event instance.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
        toast()->success('Welcome ', $user->name);
    }
}

Then

Add event like so

protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

       event(new CreateUser($user));
        return $user;
    }

Please or to participate in this conversation.