Passing variable to view results in empty variable |
OK, I am at my wit's end with this one. I have googled and tried everything I can think of. It is possibly very easy to fix, but I am unable to do so. The problem lies in the family part of the controller. Basically I want to fetch the familyId from the session and find all other members of the family from the database. The query works, but sending the data to the view does not.
I have a controller. Focus on the family-part: Code: <?php A simple view: Code: <?php If I print_r($data) from the controller I get an array: Code: Array ( [0] => Array ( [id] => 1 [email] => [email protected] [password] => $hash [created_at] => [updated_at] => [familyId] => 1 [givenName] => Andy [familyName] => H [userName] => Desde [child] => 0 ) ) But if I pass the exact same variable I get nothing coming out in the view. Can you see what is wrong with my code?
Try this..
PHP Code: public function family() PHP Code: if (empty($familydata)) (03-06-2023, 10:21 PM)HermyC Wrote: Try this..THANK YOU! I understand what I did wrong now. Big props to you!
It sounds like the issue lies in passing the data to the view. You might want to try passing the data as an array with a key, like this:
return view('family', ['data' => $data]); Then in the view, you can access the data array using the 'data' key: php <?php if (empty($data)) { echo 'Empty'; } else { print_r($data); } ?> This should allow you to access the data array in the view and display the data.
Yes, that is exactly what I did. :-)
At first I treated $data as variable, but I needed to treat $data as the container of the variables. |
Welcome Guest, Not a member yet? Register Sign In |