Alert Component
With the help of Component, it is possible to re-use a portion of the display(HTML) in multiple views. To create a component we can use the artisan make command. Below is the example where we create an Alert component. The class file of the component save on app->view->components and the display(HTML) part of the component save on resources->views/components
php artisan make:component alertComponent
Below is two parts; one for successful message and another error message. @if (session()->has('success')) check if success session variable is set, if yes display the success message. With the help of bootstrap alert div, we set in the success and error/warn.
We have set return redirect()->route('leaveTypes')->with('success', trans('message.add_success')). This is how the alert component received session message. To call a component (in our case alertComponent) we use <x->componentName>, <x-alertComponent />
@if (session()->has('success'))
<div class="alert-success">
{{ session('success') }}
</div>
@endif
@if (session()->has('warn'))
<div class="alert-danger">
{{ session('warn') }}
</div>
@endif
Nothing is change in alertComponent class file