Kuurde Kuurde - 5 months ago 34
PHP Question

Laravel redirect with data not working

My application is a simple CRUD application. I have a delete controller action which redirects back to the list when the item is successfully deleted. What I'm trying to add now is a message to the user that the data was successfully deleted.

My Controller Action:

public function deleteItem($id)
{
try {
$item = Item::findOrFail($id);
$item->delete();
return Redirect::to('list')->with('message', 'Successfully deleted');
} catch (ModelNotFoundException $e) {
return View::make('errors.missing');
}
}


The part of my list.blade.php view where I try to display the message:

@if (isset($message))
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
{{ $message }}
</div>
@endif


The problem that I have is that the
$message
variable is always empty ..

Answer Source

Since the with method flashes data to the session, you may retrieve the data using the typical Session::get method.

So you have to get as

$message = Session::get('message');