user3201500
user3201500

Reputation: 1618

Get the value of REDIRECT using WITH in laravel

we are trying to get the value sent with the redirect method in blade template in laravel.

Here is how my return statement looks like:

public function movetotrash($id){

    $page = Pages::where('id', $id) -> first();
    $page -> active = 0;
    //$page -> save();
    //return redirect()->route('pages.index')->with('trash','Page Moved To Trash');
    return redirect('pages')->with('trash','Page Moved To Trash');
}

Now once its redirect to pages it creates are URL like this http://localhost:8888/laravelCRM/public/pages.

Then i want to print the message to user that "Page is moved to trash" with $trash variable. For which i used this :

@if(isset($trash) ))
                       <div class="alert alert-error">
                          {{ $trash }}<br><br>
                       </div>
                    @endif

What should i do to print the value?

Thank you! (in advance)

Upvotes: 3

Views: 2711

Answers (3)

Mr. Tomar
Mr. Tomar

Reputation: 365

You can send data from controller like this,
You have to assign the message into variable

$trash = Page Moved To Trash';
return redirect('pages')->with('trash');

and then in view, use like this.

@if(session($trash) ))
                       <div class="alert alert-error">
                          {{ session($trash) }}<br><br>
                       </div>
                    @endif

The data will store in session and we can use it on redirect page

Hope this will help :)

Upvotes: 0

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5443

In redirect with value pass as session flash,so you need to check value like that

@if(session()->has('trash'))

then you can show like this

{{ session('trash') }}

Upvotes: 0

Bj&#246;rn
Bj&#246;rn

Reputation: 5876

The with method flashes data to the session which is only accessible in the next request. So you can retrieve it from the session like this:

@if(session()->has('trash'))
    <div class="alert alert-error">
        {{ session()->get('trash') }}
    </div>
@endif

Upvotes: 1

Related Questions