I'm new to laravel & php.
Given the code below I'd like for the user to navigate to property-specific details after clicking on a property from the list.
I'm trying to do this by sending the user to a 'property details' template which displays the specific property information dynamically so I don't have a separate page for each property.
I'm embarrassed to say how much time I've spent trying to figure this out.
//WebController.php
function properties(Request $request) //for the property list page
{
$data = array (
'title' => "Our Properties"
);
return view('p',["properties" => $this->data()])->with($data);
}
function sub_prop($id) //for the property details template page
{
$data = array (
'title' => $id
);
return view('properties.template', ['properties' => $this->data()])->with($data);
}
//web.php
Route::get('properties/{id}', 'WebController@sub_prop');
//Fake database
function data(){
return array(
array(
'name' => "Carpenter",
'address' => array(
'name' => "address", //I realize this is redundant
'street' => "address",
'city' => "city",
'zip' => "zip"
)
),
array(
'name' => "Berkman",
...etc
//property_list_page.blade.php
<div id="property-grid-2" class="container-grid ">
@foreach($properties as $property)
<div class="card-container">
<h3 class="a " id="propName">
@if(isset($property['name']))
<b>{{ $property['name'] }}</b></h3>
@else
<b>Dream Home</b></h3>
@endif
<div class="card">
<a id="propLinker" href="property/{{$property['name']}} ">
//the above href is for the dynamic link
The php inserted in the href kicks back an error of " 'name' is an undefined index." BUT the foreach loop it is in executes fine if I delete that code inside the href...
I've tried seemingly countless permutations of inserting php into the href, ajax calls with addEventListener, and more... I'm stumped. Thanks for the help.
You are sending an array to the view called $properties
. You have instructed the controller to assign $properties
with the values you have assigned to another variable called $data
:
// This tells the controller to send the var $properties with the stuff inside $data to a view called 'p'
return view('p',["properties" => $this->data()])->with($data);
You have defined the $data
array in your properties method like this:
$data = array (
'title' => "Our Properties"
);
This array (which will become $properties
) has one index, called 'title'. It doesn't have an index called 'name', thus why you are receiving the error of no index called name. It will go through the foreach loop, as there IS a $properties
variable with 'title', so that will succeed. Just no index 'name'.
Your code is a little ways off of the standard Laravel convention, which makes a lot of this stuff easier. Suggest you look into the guts of how Laravel treats controllers (tutorials like Laracasts are great), to help you more easily spot these types of issues :)