Why not?
return view('admin/facebook/show')->with('photos',$photos);
?
Thanks for your reply.
I tried your suggestion and get the error:
htmlentities() expects parameter 1 to be string, array given
Not sure why. I thought you could pass arrays as parameters in this way
Cheers
Figured it out.
The string it's expecting is a JSON string.
I used json_encode() to convert the array to json and then used:
return view('admin/facebook/show')->with('photos',$photos);
With $photos as the (json) string as expected :)
Wait a bit.. Why did you use json_encode()? Aren't you looping thru your data? Can you please post your loop in here because i can see that there is a problem in it. It has to pass array as a php array and not as a text. You are trying to output something directly and in a wrong way :)
nvaughan84 said:
Figured it out.
The string it's expecting is a JSON string.
I used json_encode() to convert the array to json and then used:
return view('admin/facebook/show')->with('photos',$photos);
With $photos as the (json) string as expected :)
Hi ModestasV,
The code in the controller looks like this:
$fb = new Facebook(); //Facebook Model
$photos = $fb->getImages(); //return array of images from Facebook
$photos = json_encode($photos); //encode $photos array to JSON
return view('admin/facebook/latest')->with('photos',$photos);
The View then looks like this
<?php
$photos = json_decode($photos);
foreach ($photos as $photo) {
echo '<img title="'.$photo->name.'" style="width:200px" src="'.$photo->source.'">';
}
?>
which converts the JSON back to an array and loops the data to output the image.
The $photos array is of the form:
Array (
[0] => Array ( [source] => image_source [name] => Image Name )
[1] => Array ( [source] => Image Source [name] => Image Name )
)
Cheers
Hi again, sorry for the long wait.
The first thing i notice you are passing an array from controller to view and then using it as object. This is why it didn't worked to you.
Do not encode the photos into a json and do not decode. Remove both of the lines and instead of using $photo->name use $photo['name']. At this point - all should work great for you!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community