Laravel Redirect Return From () Message

I am trying to redirect to the previous page with a message when there is a fatal error.

App::fatal(function($exception) { return Redirect::back()->with('msg', 'The Message'); } 

In a view, trying to access MSG with

 Sessions::get('msg') 

But nothing works, am I doing something wrong here?

+124
laravel laravel-5 laravel-4
Nov 07 '13 at 14:52
source share
17 answers

Try

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

and inside your species cause it

 @if($errors->any()) <h4>{{$errors->first()}}</h4> @endif 
+195
Nov 07 '13 at 14:55
source share

Laravel 5

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 
+104
Apr 19 '16 at 5:36
source share

Alternative approach would be

controller

 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 
+58
May 21 '14 at 21:50
source share

In Laravel 5.4, the following worked for me:

 return back()->withErrors(['field_name' => ['Your custom message here.']]); 
+16
May 03 '17 at 1:06 pm
source share

You have an error (spelling error):

 Sessions::get('msg')// an extra 's' on end 

Must be:

 Session::get('msg') 

I think it should work now, this is for me.

+13
Nov 13 '14 at 10:33
source share

Just install the flash message and redirect back from the functionality of your controller.

  session()->flash('msg', 'Successfully done the operation.'); return redirect()->back(); 

And then you can get a message in the view blade file.

  {!! Session::has('msg') ? Session::get("msg") : '' !!} 
+7
Jul 25 '16 at 8:58
source share

In Laravel 5.5 :

 return back()->withErrors($arrayWithErrors); 

In a view using Blade:

 @if($errors->has()) <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> @endif 
+7
May 25 '18 at 8:58
source share

I stopped writing this myself for laravel in favor of the Laracasts package , which handles all this for you. It is very easy to use and keeps your code clean. There is even a laracast that describes how to use it. All you need to do:

Pull the bag through the composer.

 "require": { "laracasts/flash": "~1.0" } 

Enable the service provider in app / config / app.php.

 'providers' => [ 'Laracasts\Flash\FlashServiceProvider' ]; 

Add the facade alias to the same file below:

 'aliases' => [ 'Flash' => 'Laracasts\Flash\Flash' ]; 

Pull the HTML into the view:

 @include('flash::message') 

Next to the message is a close button. It depends on jQuery, so make sure it is added before your boot file.

additional changes:

If you are not using bootstrap or want to skip enabling flash messages and write the code yourself:

 @if (Session::has('flash_notification.message')) <div class="{{ Session::get('flash_notification.level') }}"> {{ Session::get('flash_notification.message') }} </div> @endif 

If you want to view the HTML code typed by @include('flash::message') , you can find it in vendor/laracasts/flash/src/views/message.blade.php .

If you need to change partial actions:

 php artisan view:publish laracasts/flash 

The views of the two packages will now be located in the `app / views / packages / laracasts / flash / 'directory.

+4
Dec 10 '14 at 16:21
source share

I ran into the same problem and it worked.

controller

 return Redirect::back()->withInput()->withErrors(array('user_name' => $message)); 

View

 <div>{{{ $errors->first('user_name') }}}</div> 
+2
Dec 16 '15 at 5:47
source share

For Laravel 5.6. *

Trying some of the provided answers in Laravel 5.6. *, It became clear that there were some improvements that I intend to publish here in order to simplify the task for those who can not find a solution with the rest of the answers.

STEP 1:

Go to your controller file and add it before the class:

 use Illuminate\Support\Facades\Redirect; 

STEP 2: Add this to where you want to return the redirect.

  return Redirect()->back()->with(['message' => 'The Message']); 

STEP 3: Browse to the blade file and modify it as follows

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

Then check and thank me later.

This should work with laravel 5.6. * And maybe 5.7. *

+2
Mar 11 '19 at 6:38
source share

For Laravel 3

Just a head in response to @giannis christofakis; for those using Laravel 3, replace

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

from:

 return Redirect::back()->with_errors(['msg', 'The Message']); 
+1
Feb 08 '16 at 17:32
source share

Laravel 5.6. *

controller

 if(true) { $msg = [ 'message' => 'Some Message!', ]; return redirect()->route('home')->with($msg); } else { $msg = [ 'error' => 'Some error!', ]; return redirect()->route('welcome')->with($msg); } 

Blade pattern

  @if (Session::has('message')) <div class="alert alert-success" role="alert"> {{Session::get('message')}} </div> @elseif (Session::has('error')) <div class="alert alert-warning" role="alert"> {{Session::get('error')}} </div> @endif 

Enyoj

+1
Aug 04 '18 at 10:55
source share

For Laravel 5. 5+

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 
+1
Aug 01 '19 at 11:11
source share

in the controller

For example,

 return redirect('login')->with('message',$message); 

in the blade file, the Message will be stored in the session, not in a variable.

For example,

 @if(session('message')) {{ session('message') }} @endif 
+1
Aug 20 '19 at 16:04
source share

In laravel 5.8 you can do the following:

 return redirect()->back()->withErrors(['name' => 'The name is required']); 

and in the blade:

 @error('name') <p>{{ $message }}</p> @enderror 
+1
Sep 11 '19 at 12:20
source share

I received this message when I tried to redirect like:

 public function validateLogin(LoginRequest $request){ // return redirect()->route('sesion.iniciar') ->withErrors($request) ->withInput(); 

When is the right way:

 public function validateLogin(LoginRequest $request){ // return redirect()->route('sesion.iniciar') ->withErrors($request->messages()) ->withInput(); 
0
May 22 '17 at 19:05
source share

Laravel 5.8

Controller

 return back()->with('error', 'Incorrect username or password.'); 

blade

  @if (Session::has('error')) <div class="alert alert-warning" role="alert"> {{Session::get('error')}} </div> @endif 
0
Aug 10 '19 at 4:02
source share



All Articles