Home » Php » How to show Save success message in Laravel 5.1?

How to show Save success message in Laravel 5.1?

Posted by: admin October 29, 2017 Leave a comment

Questions:

In Laravel 5.1, I can save the data in the database. But I would like to show a success message. How can I do it? My code in Controller save code is

public function store(Request $request)
{

    $this->validate($request,[
        'name'=>'required|unique:seeders|max:255',
        'address'=>'required`enter code here`',
        'age'=>'required',
    ]);

    $seeder=new Seeders();
    $seeder->name=$request->input('name');
    $seeder->address=$request->input('address');
    $seeder->age=$request->input('age');
    $seeder->save();

    return redirect()->route("photo.index");
} // save data
Answers:

Just adding this code before your redirect code:

$request->session()->flash('alert-success', 'User was successful added!');

So the fully code is like here:

public function store(Request $request)
{
    // your function

    $request->session()->flash('alert-success', 'User was successful added!');
    return redirect()->route("photo.index");
}

Laravel 5.4 about Flash Data:
https://laravel.com/docs/5.4/session#flash-data

and for your view:

<div class="flash-message">
    @foreach (['danger', 'warning', 'success', 'info'] as $msg)
      @if(Session::has('alert-' . $msg))

      <p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></p>
      @endif
    @endforeach
  </div> <!-- end .flash-message -->

You can use Bootstrap Alerts view: http://www.w3schools.com/bootstrap/bootstrap_alerts.asp

Questions:
Answers:

Very easy

public function store(Request $request)
{

    $this->validate($request,[
        'name'=>'required|unique:seeders|max:255',
        'address'=>'required`enter code here`',
        'age'=>'required',
    ]);

    $seeder=new Seeders();
    $seeder->name=$request->input('name');
    $seeder->address=$request->input('address');
    $seeder->age=$request->input('age');
    $seeder->save();

    //PUT HERE AFTER YOU SAVE
    \Session::flash('flash_message','successfully saved.');

    return redirect()->route("photo.index");
} // save data

In your main view, or index
add this.

@if(Session::has('flash_message'))
    <div class="alert alert-success"><span class="glyphicon glyphicon-ok"></span><em> {!! session('flash_message') !!}</em></div>
@endif

If you want to have different flash styles check this out Flash Messages in Laravel 5

Questions:
Answers:

use the code

return redirect()->route("photo.index")->with('message','Success');

and in you template

@if(session('message'))
  {{session('message')}}
@endif

Questions:
Answers:

You can redirect user and show him the flash message. The message will be available in view in this way {{ $message }}

return redirect('user/login')->with('message', 'Success!');

More about this you can find here.

Questions:
Answers:

Show success message with close button:

Controller code:

$seeder->save();    
return redirect('photo.index')->with('status', 'You have successfully Created!');

Just paste the below code into your view file.

@if (session('status'))
        <div class="alert alert-success">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a> {{ session('status') }}
        </div>
    @endif

Questions:
Answers:

the best way -> https://github.com/laracasts/flash

just do it flash(‘Message’, ‘info’)

Leave a Reply

Your email address will not be published. Required fields are marked *