Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
class HomeController extends Controller
{
private string $api_key;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->api_key = config('flickr.API_KEY');
}
/**
* @throws RequestException
*/
public function index()
{
$collection = array_merge($this->sizeImages(), $this->getContent());
\Debugbar::info($collection);
return view('welcome', compact('collection'));
}
/**
* @return mixed
* @throws RequestException
*/
private function getContent()
{
$url = "https://www.flickr.com/services/rest/?method=flickr.photos.getPopular&api_key={$this->api_key}&user_id=23187053%40N04&extras=description%2Cowner_name%2Cviews&per_page=5&format=json&nojsoncallback=1";
$response = Http::get($url);
$responseJson = $response->json();
if ($response->successful()) {
foreach ($responseJson['photos']['photo'] as $key => $value) return $value;
}
return $response->throw();
}
/**
* @return mixed
* @throws RequestException
*/
private function sizeImages()
{
$imageSizes = "https://www.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key={$this->api_key}&photo_id={$this->getContent()['id']}&format=json&nojsoncallback=1";
$response = Http::get($imageSizes);
$responseJson = $response->json();
if ($response->successful()) {
foreach ($responseJson['sizes']['size'] as $key => $value) return $value;
}
return $response->throw();
}
}
Route
Route::get('/', [HomeController::class, 'index'])->name('home');
View
@if (isset($collection))
@foreach ($collection as $key => $item)
{{$item['source'}}
@endforeach
@endif
vardumping the $collection variable inside of the controllers index functions gives me an array
array:18 [▼
"label" => "Square"
"width" => 75
"height" => 75
"source" => "https://live.staticflickr.com/5146/5570747940_6c83ca4520_s.jpg"
"url" => "https://www.flickr.com/photos/23187053@N04/5570747940/sizes/sq/"
"media" => "photo"
"id" => "5570747940"
"owner" => "23187053@N04"
"secret" => "6c83ca4520"
"server" => "5146"
"farm" => 6
"title" => "Two times cute."
"ispublic" => 1
"isfriend" => 0
"isfamily" => 0
"description" => array:1 [▶]
"ownername" => "Cajaflez"
"views" => "8109"
]
But var dumping the $collection variable inside of my view gives me 1 array, and the rest all lose elements.
array:18 [▶]
Square
75
75
https://live.staticflickr.com/5146/5570747940_6c83ca4520_s.jpg
https://www.flickr.com/photos/23187053@N04/5570747940/sizes/sq/
photo
5570747940
23187053@N04
6c83ca4520
5146
6
times cute.
1
0
0
array:1 [▶]
Cajaflez8109
So trying to do {{$item['source'}} gives a Illegal sting offset error. in my view foreach loop.
How would I go about fixing this ?
$collection, 'source' is one of the$key. So the$itemis the link (which is a string). So there is no point of asking$item['source']