I\'m making my first app in Laravel and am trying to get my head around the session flash messages. As far as I\'m aware in my controller action I can set a flash message ei
You can pass different type of message using with
return redirect()->back()->with('error', 'Your current account is inactive ')->with('error2','please check');
To show message
@if(session('error'))
<div class="alert alert-danger" role="alert">
<p> {{session('error')}} </p>
<p> {{session('error2')}}</p>
</div>
@endif
Not a big fan of the solutions provided (ie: multiple variables, helper classes, looping through 'possibly existing variables'). Below is a solution that instead uses an array as opposed to two separate variables. It's also easily extendable to handle multiple errors should you wish but for simplicity, I've kept it to one flash message:
Redirect with flash message array:
return redirect('/admin/permissions')->with('flash_message', ['success','Updated Successfully','Permission "'. $permission->name .'" updated successfully!']);
Output based on array content:
@if(Session::has('flash_message'))
<script type="text/javascript">
jQuery(document).ready(function(){
bootstrapNotify('{{session('flash_message')[0]}}','{{session('flash_message')[1]}}','{{session('flash_message')[2]}}');
});
</script>
@endif
Unrelated since you might have your own notification method/plugin - but just for clarity - bootstrapNotify is just to initiate bootstrap-notify from http://bootstrap-notify.remabledesigns.com/:
function bootstrapNotify(type,title = 'Notification',message) {
switch (type) {
case 'success':
icon = "la-check-circle";
break;
case 'danger':
icon = "la-times-circle";
break;
case 'warning':
icon = "la-exclamation-circle";
}
$.notify({message: message, title : title, icon : "icon la "+ icon}, {type: type,allow_dismiss: true,newest_on_top: false,mouse_over: true,showProgressbar: false,spacing: 10,timer: 4000,placement: {from: "top",align: "right"},offset: {x: 30,y: 30},delay: 1000,z_index: 10000,animate: {enter: "animated bounce",exit: "animated fadeOut"}});
}
Just send an array in the session rather than a string, like this:
Session::flash('message', ['text'=>'this is a danger message','type'=>'danger']);
@if(Session::has('message'))
<div class="alert alert-{{session('message')['type']}}">
{{session('message')['text']}}
</div>
@endif
In Controller:
Redirect::to('/path')->with('message', 'your message');
Or
Session::flash('message', 'your message');
in Blade show message in Blade As ur Desired Pattern:
@if(Session::has('message'))
<div class="alert alert-className">
{{session('message')}}
</div>
@endif
You could use Laravel Macros.
You can create macros.php
in app/helpers
and include it routes.php.
if you wish to put your macros in a class file instead, you can look at this tutorial: http://chrishayes.ca/blog/code/laravel-4-object-oriented-form-html-macros-classes-service-provider
HTML::macro('alert', function($class='alert-danger', $value="",$show=false)
{
$display = $show ? 'display:block' : 'display:none';
return
'<div class="alert '.$class.'" style="'.$display.'">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong><i class="fa fa-times"></i></strong>'.$value.'
</div>';
});
In your controller:
Session::flash('message', 'This is so dangerous!');
Session::flash('alert', 'alert-danger');
In your View
@if(Session::has('message') && Session::has('alert') )
{{HTML::alert($class=Session::get('alert'), $value=Session::get('message'), $show=true)}}
@endif
I came across this elegant way to flash messages. It was made by Jeffrey Way from Laracast. check it out... https://github.com/laracasts/flash