code-8
code-8

Reputation: 58810

Can't Redirect with Error Message in Laravel

I'm trying to throw an alert message when the CPE mac is not exist. But I never seen it.

      if(!$cpe_mac){

            return view('main.lan')
            ->with('error','There is no CPE associated with this account !');

        }

2nd Try

return Redirect::to('/lan')->with('error','There is no CPE associated with this account !');

The page is stuck in the re-direct loop with this error message

enter image description here


alert.blade.php

@if ($message = Session::get('success'))

<div class="alert alert-sm alert-block alert-success">
    <button type="button" class="close" data-dismiss="alert">
        <i class="fa fa-times"></i>
    </button>
    <i class="fa fa-check green"></i>

    <strong class="green">{{ nl2br($message) }}</strong>

</div>

@elseif ($message = Session::get('error'))

<div class="alert alert-sm alert-block alert-danger">
    <button type="button" class="close" data-dismiss="alert">
        <i class="fa fa-times"></i>
    </button>

    <strong class="red">{{ nl2br($message) }}</strong>

</div>

@endif

Any clues ?

Upvotes: 0

Views: 255

Answers (1)

Fabio Antunes
Fabio Antunes

Reputation: 22882

So here's the deal this:

if(!$cpe_mac){

    return view('main.lan')
        ->with('error','There is no CPE associated with this account !');
}

Responds with your view and attaches an error variable to it, so your alert.blade.php should look like this:

//the error part
@elseif ($error)

<div class="alert alert-sm alert-block alert-danger">
    <button type="button" class="close" data-dismiss="alert">
        <i class="fa fa-times"></i>
    </button>

    <strong class="red">{{ nl2br($error) }}</strong>

</div>

@endif

So you're using session, but that error variable will never be in the session.

As for the redirect loop I can't help you at least with the code you provided but I'm guessing the problem is definitely in here:

return Redirect::to('/lan')->with('error','There is no CPE associated with this account !');

On your lan route you are redirecting over and over again, but with this approach the variable will be indeed in session, since you are redirecting and storing the data as flash message, here's the documentation about that

So summing up this:

return view('main.lan')
            ->with('error','There is no CPE associated with this account !');

Creates a variable $error and loads your view

As for this:

return Redirect::to('/lan')->with('error','There is no CPE associated with this account !');

Stores the data in session and redirects to the route provided, so on the next route you can access the data using the Session facade or the session() helper

Upvotes: 2

Related Questions