Reputation: 157
Session message is not working i tried this code and many fix available online Here id my store function `
public function store(Request $request)
{
// dd($request->all());
$this->validate($request, [
'name' =>'required',
'username' =>'required',
'email' =>'required',
'address' =>'required',
'likes' =>'required',
'gender' =>'required'
]);
$input = $request->all();
Contacts::create($input);
Session::put('flash_message', 'Task successfully added!');
return redirect()->back();
}
And Retrieving by this code
@if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
@endif
Upvotes: 5
Views: 38877
Reputation: 359
When the validation fails, no further code is executed and the previous page is loaded again. This is the reason why session message is not working. In order to check for any validation errors use the following code snippet at the top of your blade file.
@if ($errors->any())
@foreach ($errors->all() as $error)
<div class="alert alert-danger alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $error }}</strong>
</div>
@endforeach
@endif
Upvotes: 1
Reputation: 565
This will work in case "Session" fails to display errors in your blade view.
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Upvotes: 0
Reputation: 903
Try this code
Session::flash('flash_message', 'Task successfully added!');
Session::save();
return redirect()->back();
This worked for me
Upvotes: 0
Reputation: 1329
I resolved issue with laravel 6.x
As soon as I moved \Illuminate\Session\Middleware\StartSession::class
and \Illuminate\View\Middleware\ShareErrorsFromSession::class
from web $middlewareGroups
to $middleware
in app\Http\Kernel.php
everything started working as expected.
Upvotes: 24
Reputation: 2331
A bit late for this forum. I encounter this problem, I've been different sites searching for the right solution but none works. Here's my case, I'm using v6.0
, and I put the route inside routes\api.php
.
I think there is difference of putting the route
to the right place or file, can't explain more.
Here's how I solved, I transfer the route from routes\api.php
to routes\web.php
and thats it after so many researching I now successfully display the flash message.
Upvotes: 0
Reputation: 51
I RESOLVED the issue with laravel 5.2.
I was having all the routes inside this:
Route::group(['middleware' => 'web'], function() {
I remove it because when I used the command php artisan route:list in the Name Middleware column the "web" shows to times: web, web.
If you have AUTH replace by:
Route::group(['middleware' => 'auth'], function() {
Also I delete a duplicate route (in routes.php). Now I just have:
Route::resource('/publicaciones', 'PublicacionesController');
My controller:
return redirect()->back()->with('success', 'Saved!');
My view:
@if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
@endif
Upvotes: 4
Reputation: 658
I Resolved the Issue with laravel 5.2.
I was having route like this
Route::group(['middleware' => [ 'web','auth']], function () {
.......
}
So Removed the web middle ware
Route::group(['middleware' => ['auth']], function () {
.......
}
and its start working
Analysis: By default Laravel Add web Middleware. check by php artisan route:list it shows web, web,auth .
so by defining it again redirect two time for the web middleware.
Upvotes: 7
Reputation: 289
have you include the following namespace
use Session;
instead of the following code
Session::put('flash_message', 'Task successfully added!');
use
Session::flash('flash_message', 'Task successfully added!');
in instead to
return redirect()->back();
try using
return redirect()->route('your route');
Upvotes: 1