The Alpine Js Laravel Remove Flash Messages

How to remove flash messages with alpine

Hello there guys, in this tutorial will look at how to remove flash messages using the Alpine Js framework.

Let’s get started.

Step 1

Head over to this website and copy the URL of the javascript. Paste the URL inside your blade template. For mine, it should be like this…


<!DOCTYPE html>
<html lang="en">
    <title>@yield('title') - {{ config('') }}</title>
    <link rel="shortcut icon" type="image/jpg" href="{{asset('images/40 x 40 circle-cropped_image.png')}}"/>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="{{asset('css/style.css')}}">
    <link rel="stylesheet" type="text/css" href="{{ asset('css/valiadmin.css') }}" />
    <link rel="stylesheet" href="" />
    <link rel="stylesheet" href="{{asset ('css/themes/prism.css') }}">

    <link rel="stylesheet" type="text/css" href="{{ asset('css/font-awesome/4.7.0/css/font-awesome.min.css') }}"/>
<body class="app sidebar-mini rtl">
    <main class="app-content" id="app">
    <script src="{{ asset('js/jquery-3.2.1.min.js') }}"></script>
    <script src="{{ asset('js/popper.min.js') }}"></script>
    <script src="{{ asset('js/bootstrap.min.js') }}"></script>
    <script src="{{ asset('js/main.js') }}"></script>
    <script src="{{ asset('js/plugins/pace.min.js') }}"></script>
    <script src="//" defer></script> // from Alpine Js website

Remember this is a website I created before using laravel and other frontend languages. So the admin.blade.php represents the admin side of my website. You can look at this video to get the hang of it.

Step 2

Head over to your index.blade.php or whichever blade template you are using to present the admin side. For this one, it represents the admin dashboard. So have this code above.



    <div class="container">
        <div class="row">
            <div class="col">
                    <div x-data="{show: true}" x-init="setTimeout(() => show = false, 2500)" x-show="show"
                       class="alert alert-success alert-dismissible">
                        {{ Session('message') }}


From the code above the part that has the @if session is where the logic is. So what am doing is setting the show function true first to display the flash message then the show function will show false after 2500 seconds. The flash message disappears after the given set timeout.

Now you need to have the session flash message set in one of your controllers. This is what mine looks like…


 Session::flash('message', 'Post created successfully');
 return redirect()->route('posts.index');

Since this isn’t a beginner tutorial, you can watch the video below to understand it better.


Sign up for free tutorials in your inbox.

We don’t spam! Read our privacy policy for more info.

Leave a Comment

Your email address will not be published. Required fields are marked *