2016-05-22 15:26
浏览 115

Laravel 5.2重定向成功消息

I'm trying to get a success message back to my home page on laravel.

return redirect()->back()->withSuccess('IT WORKS!');

For some reason the variable $success doesn't get any value after running this code.

The code I'm using to display the succes message:

@if (!empty($success))

I have added the home and newsletter page to the web middleware group in routes.php like this:

Route::group(['middleware' => 'web'], function () {

    Route::get('/', function () {
        return view('home');


Does anyone have any idea why this doesn't seem to work?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • douqianxian7008 2016-05-22 15:30

    You should remove web middleware from routes.php. Adding web middleware manually causes session and request related problems in Laravel 5.2.27 and higher.

    If it didn't help (still, keep routes.php without web middleware), you can try little bit different approach:

    return redirect()->back()->with('message', 'IT WORKS!');

    Displaying message if it exists:

        <div class="alert alert-success">
            {{ session()->get('message') }}
    打赏 评论
  • duanji5116 2017-12-08 23:37

    you can use this :

    return redirect()->back()->withSuccess('IT WORKS!');

    and use this in your view :

    打赏 评论
  • douhuang5623 2018-05-11 00:59

    All of the above are correct, but try this straight one-liner:

    {{session()->has('message') ? session()->get('message') : ''}}

    打赏 评论
  • douju1280 2018-05-30 20:28

    One way to do that is sending the message in the session like this:


    return redirect()->back()->with('success', 'IT WORKS!');


    @if (session()->has('success'))
        <h1>{{ session('success') }}</h1>

    And other way to do that is just creating the session and put the text in the view directly:


    return redirect()->back()->with('success', true);


    @if (session()->has('success'))
        <h1>IT WORKS!</h1>

    You can check the full documentation here: Redirecting With Flashed Session Data

    I hope it is very helpful, regards.

    打赏 评论
  • dqj5046 2018-09-18 06:31

    In Controller

    return redirect()->route('company')->with('update', 'Content has been updated successfully!');

    In view

    @if (session('update'))
      <div class="alert alert-success alert-dismissable custom-success-box" style="margin: 15px;">
         <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
         <strong> {{ session('update') }} </strong>
    打赏 评论
  • douyakan8924 2019-04-14 08:20


    return redirect()->route('subscriptions.index')->withSuccess(['Success Message here!']);


    @if (session()->has('success'))
        <div class="alert alert-success">
                @foreach (session()->get('success') as $message)
                    <li>{{ $message }}</li>
                {{ session()->get('success') }}

    You can always save this part as separate blade file and include it easily. fore example:

    <div class="row">
            <div class="col-md-6">
                <div class="box box-widget">
    打赏 评论
  • dongxie8856 2019-05-25 06:00

    You can use laravel MessageBag to add our own messages to existing messages.

    To use MessageBag you need to use:

    use Illuminate\Support\MessageBag;

    In the controller:

    MessageBag $message_bag
    $message_bag->add('message', trans('auth.confirmation-success'));
    return redirect('login')->withSuccess($message_bag);

    Hope it will help some one.

    • Adi
    打赏 评论

相关推荐 更多相似问题