Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel redirect with data not working

Tags:

php

laravel

blade

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 ..

like image 428
Kuurde Avatar asked Feb 08 '14 09:02

Kuurde


2 Answers

You can use this on your blade template:

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

Reference: easylaravelblog

like image 40
Raynal Gobel Avatar answered Sep 21 '22 14:09

Raynal Gobel


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');
like image 88
Kumar V Avatar answered Sep 20 '22 14:09

Kumar V