Skip to content

Instantly share code, notes, and snippets.

@MwirabuaTimothy
Created September 30, 2015 12:19
Show Gist options
  • Save MwirabuaTimothy/b404ace0d3f2ee42e7ee to your computer and use it in GitHub Desktop.
Save MwirabuaTimothy/b404ace0d3f2ee42e7ee to your computer and use it in GitHub Desktop.
Toastr JS Middleware For Laravel 5
<?php
namespace App\Http\Middleware;
use Closure;
class Toastr
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$types = ['success', 'warning', 'info', 'danger', 'message'];
foreach ($types as $type):
if (session('flash_'.$type)):
$msg = session('flash_'.$type);
if(is_array(json_decode($msg,true))):
$msg = implode('', $msg->all(':message<br/>'));
endif;
if ($type == 'danger') $type = 'error';
session()->put('toastr.level', $type);
session()->put('toastr.message', $msg);
endif;
endforeach;
if ($errors = $request->session()->get('errors')):
$msg = '';
foreach ($errors->all() as $error):
$msg .= $error.'<br/>';
endforeach;
session()->put('toastr.level', 'error');
session()->put('toastr.message', $msg);
endif;
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment