Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Redirect Back with() Message

People also ask

WHAT IS BACK () in laravel?

Chaining Methods and Redirecting Back If you just want to redirect a user back to the previous page (the most common example – is to redirect back to the form page after data validation failed), you can use this: return redirect()->back();

How would you redirect back to a controller action?

Just set the flash message and redirect to back from your controller functiion. session()->flash('msg', 'Successfully done the operation. '); return redirect()->back(); And then you can get the message in the view blade file.


Try

return Redirect::back()->withErrors(['msg' => 'The Message']);

and inside your view call this

@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif

Laravel 5 and later

Controller

 return redirect()->back()->with('success', 'your message,here');   

Blade:

@if (\Session::has('success'))
    <div class="alert alert-success">
        <ul>
            <li>{!! \Session::get('success') !!}</li>
        </ul>
    </div>
@endif

Alternative approach would be

Controller

use Session;
       
Session::flash('message', "Special message goes here");
return Redirect::back();

View

@if (Session::has('message'))
   <div class="alert alert-info">{{ Session::get('message') }}</div>
@endif

In Laravel 5.4 the following worked for me:

return back()->withErrors(['field_name' => ['Your custom message here.']]);