Laravel 5.5 - Add flash messages to login and logout - Laravel Tutorial



Download Brave Browser


Today we are going to add flash messages to both our login and logout pages.

Here is what you need to do:

1 - Create a "Partials" folder in your views folder. In the partials folder create 2 files:

"errors.blade.php"  and add this code in it:

@if (session()->has('errors'))
    <div class="alert alert-danger text-center animated fadeIn">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <strong>
            {!! session()->get('errors') !!}
        </strong>
    </div>
@endif

And another file called "success.blade.php" with this code in it:

@if (session()->has('success'))
    <div class="alert alert-success text-center animated fadeIn">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <strong>
            {!! session()->get('success') !!}
        </strong>
    </div>
@endif

We are using a fade in effect(animated fadeIn)  from Animate.css which you can download freely from the internet(look for it).


2 - Look for the LoginController and add this code:
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Session;

class LoginController extends Controller
{
    use AuthenticatesUsers;


    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
    $this->middleware('guest')->except('logout');
    }

    protected function authenticated(Request $request, $user)
    {
    $request->session()->flash('success', 'You are logged in!');
    }


      public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->invalidate();
        $request->session()->flash('errors', 'You are logged out!');
        return redirect('/');
    }

}

3 - In your app.blade.php file add this code:


    <div class="container">
    @include('partials.errors')
    @include('partials.success')
    <div class="row">
    @yield('content')
    </div>
    </div>

Et voila!

Try to log in and out, the flash messages should not show up on your page.

What did we do?

First we created 2 partial files, what are they? Partials are here to stop repeating yourself again and again, you simply pass your message with a status "success or errors" (or whatever you call it) which will automatically guide the script to the correct partial(based on its file name).


Second, we overloaded the original functions authenticated() and logout() and added our flash messages.


Third, we have modified slightly our main app.blade.php file so the partials are included in the layout.

If you have any questions, feel free to ask!