欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Laravel redirect back with() message 有大用

Trying to redirect to the previous page with message when there is a fatal error.
App::fatal(function($exception)
{
    return Redirect::back()->with('msg', 'The Message');
}

In the view trying to access the msg with

Sessions::get('msg')

But nothing is getting rendered, am I doing something wrong here ?


9 Answers 正确答案

Try
 

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

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

and inside your view call this

@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
shareimprove this answer
 
2 
This works, how cool. But why wont this work return Redirect::back()->with('msg', 'The Message'); how to get the "msg" here ? – Mudit Tuli Nov 7 '13 at 15:25
   
Have you tried to see if the message is there? Session::has('msg') – giannis christofakis Nov 7 '13 at 15:28
   
Yes checked Session::has('msg') and the 'msg' is not there. – Mudit Tuli Nov 7 '13 at 15:33
   
Try instead of ->with() the Session::flash(). – giannis christofakis Nov 7 '13 at 15:48
1 
@giannischristofakis It sure seems like it. I have no idea what I was smoking that cold foggy morning in September. – StackOverflowed Aug 27 '15 at 14:40

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
shareimprove this answer
 

You have an error (misspelling):

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

Should be:

Session::get('msg')

I think, now it should work, it does for me.

shareimprove this answer
 

laravel 5:

 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
shareimprove this answer
 

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

Pull in the package through Composer.

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

Include the service provider within app/config/app.php.

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

Add a facade alias to this same file at the bottom:

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

Pull the HTML into the view:

@include('flash::message') 

There is a close button on the right of the message. This relies on jQuery so make sure that is added before your bootstrap.

optional changes:

If you aren't using bootstrap or want to skip the include of the flash message 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 would like to view the HTML pulled in by @include('flash::message'), you can find it in vendor/laracasts/flash/src/views/message.blade.php.

If you need to modify the partials do:

php artisan view:publish laracasts/flash

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

shareimprove this answer
 

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.

   {!! Session::has('msg') ? Session::get("msg") : '' !!}
shareimprove this answer
 

I faced with the same problem and this worked.

Controller

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

View

<div>{{{ $errors->first('user_name') }}}</div>
shareimprove this answer
 

For Laravel 3

Just a heads up on @giannis christofakis answer; for anyone using Laravel 3 replace

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

with:

return Redirect::back()->with_errors(['msg', 'The Message']);
shareimprove this answer
 

In Laravel 5.4 the following worked for me:

return back()->withErrors(['field_name' => ['Your custom message here.']]);
shareimprove this answer
 
   
What's the difference of this answer to the accepted answer? Thanks. – Lara Belle 2 days ago
   
Hi. The accepted answer is for use in Laravel 4 (see the question tag), the parameters for the withErrors() method in the answer is an array with two elements: ['msg', 'The Message']. See the api for acceptable parameters: laravel.com/api/4.2/Illuminate/Http/… – haakym 2 days ago
   
In my answer, for use in Laravel 5.4, the parameters for the withErrors() method is an array with one element that is a key => value pair, where the value is an array. 5.4 API: laravel.com/api/5.4/Illuminate/Http/… – haakym 2 days ago

来自 http://stackoverflow.com/questions/19838978/laravel-redirect-back-with-message


普通分类: