2017-03-04 114 views
1

我需要一些帮助,将laravel应用程序中的值从表单传递到另一个页面。我有一个包含区域对象数据的表格,我希望能够选择两个(或更多)区域对象,然后在新页面中查看这些对象。如何将表单数据传递给Laravel中的另一个页面

在我index.blade.php我有空鼓形式:

<form method = "POST" action="/areas/comparison" id="preferencesForm" class="form-group"> 
    !{csrf_field()}! 
     <table id='areas_table' class="table table-striped"> 
      <thead class="thead-default"> 
       <tr> 
        <th>Select</th> 
        <th>Area</th> 
        <th>Overall Score</th> 
        <th>Housing Affordability Ratio</th> 
        <th>Mean House Price</th> 
        <th>Crime Level</th> 
        <th>Green Space</th> 
        <th>Good GCSE's</th> 
        <th>Number of Pubs &amp; Restaraunts:</th> 
        <th>Superfast Broadband</th> 
       </tr> 
      </thead> 
      <tbody> 
      @foreach ($areas as $area) 
       <tr> 
        <td scrope="row"><input type="checkbox" name="area[]" value="!{$area->id}!"></td> 
        <th><a href="/areas/!{$area->id}!">!{$area->name}!</a></th> 
        <td>!{Helpers::calculateOverallScore($area)}!</td> 
        <td>!{$area->housing_affordability_ratio}!</td> 
        <td>£!{$area->mean_house_price_2015}!</td> 
        <td>!{$area->crime}!</td> 
        <td>!{$area->greenspace*100}!%</td> 
        <td>!{$area->five_good_gcses*100}!%</td> 
        <td>!{$area->restaurants}!/km<sup>2</sup></td> 
        <td>!{$area->superfast_broadband*100}!%</td> 
       </tr> 
      @endforeach 
     @endif 
      </tbody> 
     </table> 
     <input type="hidden" name="_token" value="{{ csrf_token() }}"> 
     <input type="submit" id="compare_button" class="btn btn-primary" value="Compare"/> 
</form> 

而且我想,然后去show_comparison.blade.php并能够显示在两个数据(或更多)我选择的区域。

在routes.php文件,我有:

Route::post('/areas/comparison', '[email protected]_comparison'); 

然后在AreasController:

public function show_comparison(Area $area) 
{ 
    $formData = Request::all(); //Get the form data with the facade 

    return view('areas.show_comparison', compact('formData')); 
} 

然而,当我点击提交,并试图带我去show_comparison.blade.php它返回错误消息“尝试获取非对象的属性”。

这里是show_comparison.blade.php: @extends( '布局')

@section('content') 
<div class="container"> 
    <a href="/areas" class="btn btn-primary">Back</a> 
    <h1>Comparison</h1> 
    <p>Hello, this is the comparison page. !{$formData}!</p> 
</div> 
@stop 

我非常希望能在形式打印数据{area1->名}!和!{area2-> name}!根据我的选择。

如果任何人都可以展示我应该如何将选定数据传递到另一个令人惊叹的页面。 (感谢您花时间阅读本文。)

+0

你能告诉我们你的'show_comparison.blade.php'代码 –

+0

我已经添加了视图。 –

回答

0

我在另一个论坛上得到了答案。此解决方案将采用表单传递它的id,然后将获取相应的区域对象并将其传递到show_comparisons.blade.php视图。

public function show_comparison(Request $request) 
{ 

$areas= Area::whereIn('id',$request->area)->get(); 

    return view('areas.show_comparison', compact('areas')); 
} 
相关问题