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?
Try form will be if you using POST method why setting variable in route it will get directly on your function with post data.
route :-
controller function :-
and get data on view2 :-
For more follow HERE
First you should change your
postView
function into:And your route:
into:
Now, you should change your
view2
function into:Now in your
view2.blade.php
you should be able to use:Here's what other answers are missing, straight from Laravel docs:
So instead of
{{$name}}
write{{Session::get('name')}}
.You need to name the variable: