Laravel 5.2。* redirect-> back->没有工作

I want to write some data to my database, if the query fails I want to display an error. I tried the following:

return redirect()->back()->with('data', ['Database Error!']);

The redirect works great, but I cant read out the response in $data

My Blade:

@if (session()->has('data'))
<div class="alert alert-danger" role="alert">...</div>
@endif

//Second try
<?php if(session('data')) echo $message; ?>

But both methods doesn't work, i read a lot of L5.2 Docs but nothing works.

Do i need to modify the session config?!? Or what is the problem?

This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for display the Session message in all your views is not being utilized because it was moved from the global middleware to the web middleware group.

There are two ways to fix this:

  1. In your kernel.php file(app/Http/Kernel.php), you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.
  2. Wrap all your web routes with a route group and apply the web middleware to them:

    Route::group(['middleware' => 'web'], function() {
        // Place all your web routes here...(Cut all `Route` which are define in `Route file`, paste here) 
    });
    

try return back()->with('data', ['Database Error!']);

back() will take you to the previous page with a data variable that holds 'Database Error!'