Reputation: 487
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
Upvotes: 25
Views: 96045
Reputation: 5499
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
Upvotes: 3
Reputation: 1076
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
Upvotes: 87
Reputation: 29
the best way -> https://github.com/laracasts/flash
just do it flash('Message', 'info')
Upvotes: 2
Reputation: 281
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">×</a> {{ session('status') }}
</div>
@endif
Upvotes: 7
Reputation: 1167
use the code
return redirect()->route("photo.index")->with('message','Success');
and in you template
@if(session('message'))
{{session('message')}}
@endif
Upvotes: 17
Reputation: 2570
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
Upvotes: 12