2017-03-04 19 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')); 
} 
相關問題