1

Controller

public function codingPuzzleProcess()
{

    $word     = Input::get('word');
    $length   = strlen($word);


    return Redirect::to('/coding-puzzle')
        ->with('word', $word )
        ->with('length', $length )
        ->with('success','Your word was submit succesfully!');

}

View

I tried to access those data in my blade view like this

{!!$word or '' !!} | {!!$length or '' !!}

and I'm not sure why I got nothing printing. I'm sure that my

$word = 'love' with length of 4

Any hints / suggestion on this will be much appreciated !

8
  • Sure, I'll try that. Commented Sep 29, 2015 at 14:06
  • I got blank outputs. Commented Sep 29, 2015 at 14:06
  • Is this a GET or POST request? Commented Sep 29, 2015 at 14:06
  • 1 with(), still doesn't display anything. Commented Sep 29, 2015 at 14:07
  • It's a GET. I think. I will update my post with my route declaration for you for clarification. Commented Sep 29, 2015 at 14:08

3 Answers 3

3

A redirect doesn't pass control to a view, it passes control to the client, which then issues another, separate request, to the application.

This request will then be handled by a controller which can send data to a view like I think you are trying to. However, when you redirect and pass data like this, the data will not just become magically available in the current scope. It will instead be magically available in the session flash data:

{!! $word = session('word') ? $word : '' !!}

Manual

Sign up to request clarification or add additional context in comments.

2 Comments

My word is aaa, length = 3 , how comes when I do {!! session('word') or '' !!} | {!!session('length') or '' !!} I got 1 | 1
I think because the result of $a or $b is a boolean value. Try just {!! $word = session('word') ? $word : '' !!}
1

You got confused between the with() method of the redirect response and the with() method of the view class. Although similar they don't do the same. The first one, defined in Illuminate\Http\RedirectResponse flashes the values to the session. The second one, defined at Illuminate\View\View, passes the values to the view.

Since you are returning a Redirect response, you values will not be directly accessible as variables in your view. You will need to use the session() or old() helper to access those values from your views.

Comments

1

First at all, you don't need to redirect, you can use a return view statement and send the data, like this:

return view('coding-puzzle', ['word' => $word, 'lenght' => $lenght, 'success' => 'Your word was submit succesfully!']);

Then in your view you can just access these variables in the way that you want: {{$word}} ("scaping" the variable value or {!!$word!!} to do not scape it.

Now, iy you really need to do a redirection, so you can't access these variables in the "normal" way, you need to access it through the session, like this:

@if(Session::has('word'))
    {{Session::get('word')}}
@endif

And in the same way with the others.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.