简体   繁体   中英

How to show Save success message in Laravel 5.1?

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

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

use the code

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

and in you template

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

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

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 .

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

In Laravel 5.5 you can use...

In your controller action:

...
return redirect('photo.index')->with('alert-success', 'The data was saved successfully');

Then in your template you can use the session helper:

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

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

just do it flash('Message', 'info')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM