9

I'm trying to pass a variable from one view to a controller to another view. I'm not getting any errors, but when it gets to the last view, it doesn't show the variable like it's supposed to. In the first view, I'm just getting a name.

{{ Form::open(array('route' => 'form', 'method'=>'post')) }}
    {{ $name = Form::text('name') }}
    {{ Form::submit('Go!') }}
{{ Form::close() }}

Here is my HomeController.php.

public function view1()
{
    return View::make('stuff');
}

public function postView1($name)
{
    return Redirect::route('view2')->with($name);
}

public function view2($name)
{
    return View::make('view2')->with($name);
}

routes.php

Route::get('/', array('as' => 'stuff', 'uses' => 'HomeController@stuff'));
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController@postView1'));
Route::get('view2/{name}', array('as' => 'view2', 'uses' => 'HomeController@view2'));

view2.blade.php

{{ $name = Input::get('name') }}
<p> Hello, {{ $name }} </p>

So why isn't it showing up?

porcupine92
  • 123
  • 2
  • 2
  • 5

5 Answers5

14

First you should change your postView function into:

public function postView1()
{
    return Redirect::route('view2', ['name' => Input::get('name')]);
}

And your route:

Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController@postView1'));

into:

Route::post('form', array('as' => 'form', 'uses'=>'HomeController@postView1'));

Now, you should change your view2 function into:

public function view2($name)
{
    return View::make('view2')->with('name',$name);
}

Now in your view2.blade.php you should be able to use:

<p> Hello, {{ $name }} </p>
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • That makes sense now. But I'm still having a problem. Now, when it gets to view2, it just says "Hello, {name}" instead of actually showing the name. – porcupine92 Oct 14 '14 at 07:50
  • @porcupine92 And what exactly URL you put into browser for that? You should use for example `http://localhost/yourproject/view2/porcupine92` – Marcin Nabiałek Oct 14 '14 at 07:53
  • When I type a name into the text field for the form and click the submit button, it goes to view2 and just says "Hello, {name}". The URL just says `http://localhost/myproject/view2/%7Bname%7D`. If I type `http://localhost/yourproject/view2/porcupine92` for the URL, it just says "Hello, " with no name at all. – porcupine92 Oct 14 '14 at 08:05
  • @porcupine92 I've edited my answer, now data is injected as route parameter – Marcin Nabiałek Oct 14 '14 at 08:10
  • I'm getting a "NotFoundHttpException" now. Could that be because of a problem with my routes? – porcupine92 Oct 14 '14 at 08:24
  • @porcupine92 And what url you use now when you get this exception? You should be precise and make sure code is exactly the same as I showed. – Marcin Nabiałek Oct 14 '14 at 08:27
  • My code looks exactly like yours. When I use the url `http://localhost/myproject/view2/porcupine92`, it does say "Hello, porcupine92". But why won't it do that when I use the submit button? Thank you so much for your help so far! – porcupine92 Oct 14 '14 at 08:43
  • Thank you so much! That worked. Hopefully someday I'll be as good as you with PHP. – porcupine92 Oct 14 '14 at 09:00
2

You need to name the variable:

public function view2($name)
{
    return View::make('view2')->with('name', $name);
}
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • I tried that, and it still didn't work. Do you have any other ideas about what could be wrong with what I'm doing? I've been trying to figure this out for a while now, and I just can't get it to work. – porcupine92 Oct 14 '14 at 06:14
2
class HomeController extends Controller {
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    public function index()
    {
        $data = array (
            'title'=>'My App yo',
            'Description'=>'This is New Application',
            'author'=>'foo'
        );
        return view('home')->with($data);;
    }
}
jsanc623
  • 534
  • 4
  • 17
Sumesh Ps
  • 91
  • 8
0

Try form will be if you using POST method why setting variable in route it will get directly on your function with post data.

{{ Form::open(array('url' => 'form', 'method'=>'post')) }}
    {{Form::text('name') }}
    {{ Form::submit('Go!') }}
{{ Form::close() }}

route :-

Route::post('form','HomeController@postView1');

controller function :-

public function postView1() {
  $data = Input::all();
  return Redirect::route('view2')->with('name', $data['name']);
}

and get data on view2 :-

<p> Hello, {{ $name }} </p>

For more follow HERE

Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
-1

Here's what other answers are missing, straight from Laravel docs:

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

So instead of {{$name}} write {{Session::get('name')}}.

cha-cha
  • 328
  • 3
  • 13