表单 - 将数组从控制器传递到视图 - PHP - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26251108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form - Passing array from controller to view - PHP - Laravel
提问by porcupine92
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.
我对 Laravel 真的很陌生,我不确定我知道我在做什么。我的主视图中有一个表单。我将输入传递给控制器,并且我希望数据显示在另一个视图中。我似乎无法将数组从控制器获取到第二个视图。我不断收到 500 hphp_invoke。这是我将数组从控制器传递到 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
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.
我的 view2.php 文件非常简单。
<?php
echo $name;
echo $age;
?>
Then in routes.php
然后在routes.php
Route::get('/', function()
{
return View::make('view1');
});
Route::post('view2', 'controller@formSubmit');
Why isn't this working?
为什么这不起作用?
回答by Rakesh Sharma
try with()
尝试()
$data = array(
'name' => $name,
'age' => $age
);
return View::make('view2')->with($data);
on view get :- echo $data['name']; echo $data['age'];
在视图中获取:- echo $data['name']; 回声 $data['age'];
or
或者
return View::make('view2')->with(array('name' =>$name, 'age' => $age));
get on view :-
进入视野:-
echo $name;
echo $age;
For more Follow here
更多请关注这里
回答by Marcin Nabia?ek
You need to use:
您需要使用:
return View::make('view2')->with(['name' => $name, 'age' => $age]);
to use
使用
$name
and $age
in your template
$name
并$age
在您的模板中
回答by The Alpha
Since $things
is already an array
so you may use following approach but make the array associative:
由于$things
已经是一个,array
因此您可以使用以下方法但使数组关联:
$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:
因此,您可以访问$name
和$age
在您的view
. 另外,你可以试试这个:
return View::make('view2')->with('name', $name)->with('age', $age);
回答by johnieje
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