Search code examples
laravellaravel-5flash-message

Flash data in Laravel 5


I'm trying to display flash data but it's not showing properly. It's showing:

{{ Session::get('flash_message') }}

but it should be the message

"Your article has been created"

What's wrong with my code? Thanks!

In my controller I have:

public function store(ArticleRequest $request) 
{ 
    Auth::user()->articles()->create($request->all());

    \Session::flash('flash_message', 'Your article has been created');

    return redirect('articles');            
}

My app.blade.php is:

<!DOCTYPE html>
    <head>
    <meta charset="UTF-8">
        <title>App Name - @yield('title')</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <link rel="stylesheet" href="{{ elixir('css/all.css') }}">
        <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script src="js/app.js"></script>
    </head>
    <body>

        <div class="container">

            @if(Session::has('flash_message'))
                <div class="alert alert-success">{{ Session::get('flash_message') }}</div>
            @endif

            @yield('content')

        </div>

        @yield('footer')

    </body>
</html>

In my route.php I have the following: Curly braces display content as string not variables.

<?php
Blade::setContentTags('<%', '%>'); // for variables and all things Blade
Blade::setEscapedContentTags('<%%', '%%>'); // for escaped data

Route::get('/', function() {
	return 'Home Page';
});

Route::get('blade', function () {
    return view('about');
});

   
Route::get('about', 'HelloWorld@about');

Route::get('foo', ['middleware' => 'manager', function() {
	return 'this page may only be viewed by managers';
}]);
   

Route:resource('articles', 'ArticlesController');

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

]);


Solution

  • If you have this in your route.php:

    Blade::setContentTags('<%', '%>');
    

    then that means you cannot use curly brackets for blade content. Try this instead:

    @if(Session::has('flash_message'))
        <div class="alert alert-success">
            <% Session::get('flash_message') %>
        </div>
    @endif
    

    or simply remove the setContentTags() call from your route.php.