Reputation: 31
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 & 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.)
Upvotes: 2
Views: 5284
Reputation: 31
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'));
}
Upvotes: 1