Support the ongoing development of Laravel.io →
posted 9 years ago
Views
Last updated 2 years ago.
0

Why not?

      return view('admin/facebook/show')->with('photos',$photos);

?

Last updated 9 years ago.
0

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

0

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 :)

0

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 :)

Last updated 9 years ago.
0

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

0

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!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

nvaughan84 nvaughan84 Joined 28 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.