douji3623 2017-03-04 21:40
浏览 26
已采纳

如何将表单数据传递到Laravel中的另一个页面

I need some help in my laravel application with passing the values from a form though to another page. I have a table with data on area objects, and I want to be able to select two (or more) area objects, and then view those in a new page.

In my index.blade.php I have the hollowing form:

<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>

And I would like to then go to show_comparison.blade.php and be able to display the data on the two (or more) areas that I selected.

In routes.php I have:

Route::post('/areas/comparison', 'AreasController@show_comparison');

And then in AreasController:

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

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

However when I click submit and it tries taking me to show_comparison.blade.php it returns the error message "Trying to get property of non-object".

Here is show_comparison.blade.php: @extends('layout')

@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

Ideally I would like to be able to print the data in the form of !{ area1->name}! and !{area2->name}! according to my selection.

If anyone can show my how I should be passing form selected data through to another page that would be amazing. (Thank you for taking the time to read this.)

  • 写回答

1条回答 默认 最新

  • doucan1996 2017-03-07 22:34
    关注

    I was given an answer on another forum. This solution will take the id that the form passes it, then will fetch the corresponding area objects and pass these in an array to the show_comparisons.blade.php view.

    public function show_comparison(Request $request)
    {
    
    $areas= Area::whereIn('id',$request->area)->get();
    
        return view('areas.show_comparison', compact('areas'));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目