Reputation: 123
I'm really new to Laravel, and I'm not sure that I know what I'm doing. I have a form in my main view. I'm passing the input to a controller, and I want the data to be displayed in another view. I can't seem to get the array from the controller to the second view. I keep getting 500 hphp_invoke. Here's where I'm passing the array from the controller to view2.
public function formSubmit()
{
if (Input::post())
{
$name = Input::get('name');
$age = Input::get('age');
$things = array($name, $age);
return View::make('view2', array('things'=>$things));
}
}
view1.blade.php
{{ Form::open(array('action' => 'controller@formSubmit')) }}
<p>{{ Form::label('Name') }}
{{ $name = Form::text('name') }}</p>
<p>{{ Form::label('Age') }}
{{ $age = Form::text('age') }}</p>
<p>{{ Form::submit('Submit') }}</p>
{{ Form::close() }}
My view2.php file is really simple.
<?php
echo $name;
echo $age;
?>
Then in routes.php
Route::get('/', function()
{
return View::make('view1');
});
Route::post('view2', 'controller@formSubmit');
Why isn't this working?
Upvotes: 3
Views: 37854
Reputation: 21
In your controller, use
return View::make('view2')->with($things);
In your view, you can now access each attribute using
@foreach($things as $thing)
<p>{{ $thing->name }}</p>
<p>{{ $thing->age }}</p>
@endforeach
Upvotes: 2
Reputation: 146201
Since $things
is already an array
so you may use following approach but make the array associative:
$name = Input::get('name');
$age = Input::get('age');
$things = array('name' => $name, 'age' => $age);
return View::make('view2', $things);
So, you can access $name
and $age
in your view
. Also, you may try this:
return View::make('view2')->with('name', $name)->with('age', $age);
Upvotes: 2
Reputation: 13728
try with()
$data = array(
'name' => $name,
'age' => $age
);
return View::make('view2')->with($data);
on view get :- echo $data['name']; echo $data['age'];
or
return View::make('view2')->with(array('name' =>$name, 'age' => $age));
get on view :-
echo $name;
echo $age;
For more Follow here
Upvotes: 7
Reputation: 111859
You need to use:
return View::make('view2')->with(['name' => $name, 'age' => $age]);
to use
$name
and $age
in your template
Upvotes: 3