Laravel 5.2 redirect back with success message

前端 未结 9 2149
忘掉有多难
忘掉有多难 2020-12-13 01:50

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

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

For some

相关标签:
9条回答
  • 2020-12-13 02:09

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

    Controller:

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

    View:

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

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

    Controller:

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

    View:

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

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

    I hope it is very helpful, regards.

    0 讨论(0)
  • 2020-12-13 02:12

    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>
      </div>
    @endif
    
    0 讨论(0)
  • in Controller:

     `return redirect()->route('car.index')->withSuccess('Bein ajoute')`;
    

    In view

      @if(Session::get('success'))
               <div class="alert alert-success">
                   {{session::get('success')}}
               </div>
       @endif
    
    0 讨论(0)
  • 2020-12-13 02:16

    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
    0 讨论(0)
  • 2020-12-13 02:28

    you can use this :

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

    and use this in your view :

    @if(session('success'))
        <h1>{{session('success')}}</h1>
    @endif
    
    0 讨论(0)
  • 2020-12-13 02:29

    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:

    @if(session()->has('message'))
        <div class="alert alert-success">
            {{ session()->get('message') }}
        </div>
    @endif
    
    0 讨论(0)
提交回复
热议问题