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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀