I want a show a flash message in my login page user register and
redirect back with success message to login page how to show flash message in a login page
this is the function RegisterController.php
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
return redirect($this->redirectPath())->withMessage('message', 'my msg');
}
login.blade.php
@if (session('message'))
<div class="alert alert-success" role="alert">
{{ session('message') }}
</div>
@endif
When you use withMessage('message', 'my msg')
you already gave the key to your message. So either use just withMessage('my msg')
or ->with('message', 'my msg')
.
And noticing the other answers using the flash
separately, but with
already uses flash
session.
Add Session::flash('message', 'Your message!');
to your controller.
Change in your controller like this:
return redirect($this->redirectPath())->with('message', ['my msg']);
You can try bellow code:
In controller:
use Session;
Session::flash('success','Success Message.');
Session::flash('danger','Error Message.');
Session::flash('warning','Warning Message.');
Session::flash('info','Info Message.');
In blade file:
@foreach (['danger', 'warning', 'success', 'info'] as $msg)
@if(Session::has('alert-' . $msg))
<p class="success text-{{ $msg }}">{{ Session::get('alert-' . $msg) }}</p>
@endif
@endforeach