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">×</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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With