I'm trying to redirect the user after the create wizard
is complete back to the main page.
If I write in my controller return view('main')->with('key'=>'data')
, all is fine, but the URL does not change to localhost:8080
but it stays as 'localhost:8080/wizard/finish`.
If I use redirect('/')->with(['message' => 'Done.'])
it redirects to the main page, but because there is already a Route::get('/','Controller')
, the controller is triggered and it returns my default main page with no message.
<div class="row">
<div class="col-12">
@if (isset($message))
<p align="center" style="color: red;"><strong>{{$message}}</strong></p>
@endif
</div>
</div>
EDIT: With a breakpoint in the MainPageController
(mapped to /
), I see that this controller is triggered when there is a redirect to the /
route. Thus, I lose the $message
, as the MainPageController
also returns the same view, but with no message.
Try this
return redirect('/url')->with('var', 'value');
When you're redirecting, Laravel uses the session to keep the message between requests.
return redirect('/')->with('message', 'Done.');
So, to display the message change this:
@if (isset($message))
<p align="center" style="color: red;"><strong>{{$message}}</strong></p>
@endif
To this:
@if (session()->has('message'))
<p align="center" style="color: red;"><strong>{{ session('message') }}</strong></p>
@endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With