Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Save success message in Laravel 5.1?

Tags:

php

laravel

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
like image 838
Myat Htut Avatar asked Jun 21 '15 05:06

Myat Htut


4 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

like image 103
Ridwan Pujakesuma Avatar answered Nov 15 '22 10:11

Ridwan Pujakesuma


use the code

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

and in you template

@if(session('message'))
  {{session('message')}}
@endif
like image 23
Naumov Avatar answered Nov 15 '22 10:11

Naumov


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

like image 25
Ikong Avatar answered Nov 15 '22 08:11

Ikong


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.

like image 21
Viktor Avatar answered Nov 15 '22 09:11

Viktor