3

Good day to all, I was creating CRUD operation for my project and wanted to ensure that after user creates new record he or she would redirect to a certain page and with success message thus I decided to use withSuccess() but even if new record is added success message is not shown. The code I use:

Route::post('/contact/submit', function (Request $request) {
    $contact=new Contact();
    $contact->FirstName=$request->input('FirstName');
    $contact->LastName=$request->input('LastName');
    $contact->Age=$request->input('Age');
    $contact->save();
   return redirect('/contact')->withSuccess('Created');
});
akshaypjoshi
  • 1,245
  • 1
  • 15
  • 24
Mirasan
  • 259
  • 1
  • 4
  • 16

4 Answers4

3

you can use like this

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

or

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

or

Session::flash('message','IT WORKS!'); //<--FLASH MESSAGE

return redirect('/contact');

and to display data on view use like this

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

use like this to display on view if you used withSuccess() method

@if(session('success'))
    <h1>{{session('success')}}</h1>
@endif
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
  • thank you dude for your great solution, but look why with('success') works properly with controllers, that is, if I use resource controller for CRUD then I do not have to add the code that you provided and all works properly – Mirasan Sep 01 '18 at 08:37
  • come on dude if it weren't important, I wouldn't ask your explanation – Mirasan Sep 01 '18 at 09:09
  • @Mirasan it may me a middleware issue, add the web middleware for your route and then check – RAUSHAN KUMAR Sep 01 '18 at 09:13
2

You need to add this in your view file:

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

Check here : Laravel 5.2 redirect back with success message

RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
akshaypjoshi
  • 1,245
  • 1
  • 15
  • 24
0
Follow the below steps:

1) In view file

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


2) In controller function:
use with() function in place of withSuccess()

return redirect('/contact')->with('success', 'Information has been added Successfully!!');
Sunil Sharma
  • 164
  • 2
  • 9
0

I was using named routes for redirects. In case anyone finds themselves facing the same problem, using the route() method seems to redirect correctly and flash the message.

return redirect(route('my-teams.index'))->withSuccess('Success message.');

The issue I encountered is that, it is possible to use the route name directly, but it does not flash the success message. The following code redirects correctly without flashing the message:

return redirect('my-teams.index')->withSuccess('Success message.');
Aridez
  • 452
  • 5
  • 17