Hi friend's i need help how to display success message without using session
Just adding this code before your redirect code:
$request->session()->flash('alert-success', 'User was successful added!');
Laravel 5.1 about Flash Data : http://laravel.com/docs/5.1/session#flash-data
and for your view:
<div class="flash-message">
#foreach (['danger', 'warning', 'success', 'info'] as $msg)
#if(Session::has('alert-' . $msg))
<p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} ×</p>
#endif
#endforeach
</div> <!-- end .flash-message -->
You can use Bootstrap Alerts view: http://www.w3schools.com/bootstrap/bootstrap_alerts.asp
Append a variable name in URL like:
xyz.com/backend/packages?status=true
At your view get the url components and check if that variable exist:
if(isset($status))
{
echo '<script>alert("You message")<script>';
}
If you want to display any messages between requests without using session, you'll need to store these messages somewhere before a redirect. For example, you could use DB:
Message::create([
'message' => 'Thanks for adding the comment',
'user_id' => auth()->user()->id
]);
And get it after redirect and then delete it:
$messages = Message::where('user_id', auth()->user()->id)->get();
Message::destroy($messages->pluck('id'));
Alternatively you could use files, Redis etc as Laravel itself does when working with sessions.
Related
I am have trouble figuring out why laravel validation error message are not showing in my current blade view file when I try to send a post request without an title input.
I would get a 422 post error.
upload.blade.php
#if (count($errors) > 0)
<div class = "alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<input type="text" name="title" id="title" placeholder="enter post title" />
uploadcontroller.php
public function store(Request $request)
{
$photos = $request->file('file');
$title = $request->input('title');
$this->validate($request, [
'title' => 'required|max:120',
]);
}
When I inspect the 422 POST error, only then it shows the error message
If you are using a standard form, you need to return back to the view in order to see the error. Something like:
return \Redirect::back() // can send errors, or with() or whatever
If you are using ajax to send your info, you'll need to handle the response via the success or error method, depending on how you wish to handle the error. Your store() method would then send some kind of text back to ajax in this case.
Bottom line, you need to return something from your store() method to get back to the view you were on, else you won't see anything unless you inspect it.
I'm using the following exception handler in the handler.php file :
public function render($request, Exception $e)
{
if ($e instanceof CustomException) {
return response()->view('errors.404', [], 404);
}
return parent::render($request, $e);
}
It is working fine until I include the header to the 404.blade.php file :
#include('includes.header')
I start receiving this error :
Session store not set on request. (View: C:\xampp\htdocs\sharp\resources\views\includes\header.blade.php)
Because the route wasn't found, the web middleware, which starts the session, was never applied.
I'm not a fan of this answer, but it is relevant.
Instead, consider passing a parameter to your nested template specifying if the section of your template calling Auth::user() should be rendered, via the #if blade directive.
Something like:
#include('includes.header', ['omit_auth' => true])
then in your header template:
#if (!empty($omit_auth))
{{ Auth::user()->name }}
#endif
I'm trying to flash error messages from my controller back to my view. I tried this with:
\Route::group(['middleware' => 'web'], function ()
flash('Error message');
return Redirect::back();
});
And tried showing it my view with:
#include('flash::message')
However this just seems not to show the message.
I've been looking over the web for some good 2 to 3 hours now and I am at a loss right now.
If this is a duplication of another question somewhere on stackoverflow, then sorry!
To use session flash in Laravel:
web.php
Route::get('/',
function () {
Session::flash('error', 'test');
return view('welcome');
});
In your .blade view file you can access the message using
#if (session('error'))
<div class="alert alert-warning">{{ session('error') }}</div>
#endif
You could replace 'error' with any type of message ('success', 'warning', 'yourOwnMessageIdentifier etc) you'd want to flash.
In controller
use Session;
\Session::flash('msg', 'Error' );
in blade
{!!Session::get('msg')!!}
use simply
\Session::flash('msg', 'Changes Saved.' );
#if(Session::has('msg'))
<div class="alert alert-info">
<a class="close" data-dismiss="alert">×</a>
<strong>Heads Up!</strong> {!!Session::get('msg')!!}
</div>
#endif
I'm having trouble showing flash messages with Phalcon PhP. Here is how I register the service:
use Phalcon\Flash\Direct as Flash;
$di->set('flash', function () {
return new Flash(array(
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
'warning' => 'alert alert-warning'
));
});
In my controller I add the flash message like this
$this->flash->success('The carrier was successfully activated');
In my view I try to show like this (volt):
{{ flash.output() }}
My layout has the {{ content() }} tag and I have tried to apply the discussed in this post but it doesn't work anyway.
Can you see what I'm missing here? Thanks for any help!
Your are using the wrong flash session. Instead of
use Phalcon\Flash\Direct as Flash;
Use
use Phalcon\Flash\Session as Flash;
The documentation says:
Flash\Direct will directly outputs the messages passed to the
flash.
Flash\Session will temporarily store the messages in
session, then messages can be printed in the next request
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
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.
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.
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
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');
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
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.
Try this code
Session::flash('flash_message', 'Task successfully added!');
Session::save();
return redirect()->back();
This worked for me
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