Following HTML with CSS class is used to display the error or any flash message if exists. Usually it is kept inside _partials folder. Then such file can be called from any where by specifying its path.
HTML & CSS With Class
FileName: flash-message.blade.php
@if ($errors->any())
<div class="alert alert-danger" style="margin-bottom:0px">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if(request()->session()->has('success'))
<div class="alert alert-success" style="margin-bottom:0px">
<h4> {{ request()->session()->get('success') }} </h4>
</div>
@endif
@if(request()->session()->has('warning'))
<div class="alert alert-warning" style="margin-bottom:0px">
<h4> {{ request()->session()->get('warning') }} </h4>
</div>
@endif
@if(request()->session()->has('danger'))
<div class="alert alert-danger" style="margin-bottom:0px">
<h4> {{ request()->session()->get('danger') }} </h4>
</div>
@endif
To include these code on inside any file if error or flash session exist
@include('admin._partials.flash-message')
To set error or flash inside the controller
return redirect()->back()->withSuccess("Service inserted successfully");
@if(request()->session()->has('success'))
withSuccess();
withWarning();
withDanger();
Anything appended with word 'with' is automatically taken as session variable. i.e. withSuccess means success as session variable and withWarning means warning as session variable and same with withDanger. Anything written inside parenthesis is assigned value to that particular session name.
@if ($errors->any())
Any errors that existing while validating after submitting the form will automatically creates value inside the array called $error
Set Session Value in different ways
Method 1: Using Session Facade
\Illuminate\Support\Facades\Session::put('name', 'my values');
Method 2: Via a request instance...
$request->session()->put('key', 'value');
Method 3: Via the global helper...
session(['key' => 'value']);