warpig's avatar
Level 12

Only getting the initial position from an associative array

I can't make a youtube link to be dynamic by passing the position of their array's location in the Collection.

First, I make a GET request to an API like this:

    public function thps4($id)
    {
        $id++;
        $thps4Videos = Http::get('http://thvid-api.herokuapp.com/videos/game/THPS4/$id/1')->json();
    
        $thps4Video = $thps4Videos[0];
    
        return view ('videos.thps4', compact('thps4Video'));
    }

In a forelse I iterate through the information but also I pass this data with a $key variable, and I have an href=""

<a class="relative bottom-4" href="videos/thps4/{{ $key }}">

This URL, it displays something like this: thps4/videos/0 and that should point to the array in the 0 position, and this number changes for other items currently being displayed.

On the end point of this URL I am passing the variable $thps4Video and I use the ID column and insert it on the Youtube URL like this:

	"src": "http://www.youtube.com/watch?v={{ $thps4Video['ID'] }}"}],

It is a json object that accepts many properties, one of them being a Youtube link, if more info should be provided do tell please.

I should be able to see a different video everytime I click on a link provided by that href but I always see the same one.

It's a Laravel 8 project and im using VideoJS version 7.11.8 with the Youtube extension (wonderful)

A little bit about the API

This API actually fetches data from Youtube videos and fetches that metadata to it's own endpoint (http://thvid-api.herokuapp.com) so any column that exists here also does on Youtube. They all have a column named "_id", but also one called "ID" which is the ID that corresponds for every video, the former seems to be the id of the array, I think.

0 likes
1 reply
warpig's avatar
Level 12

I was just watching this: https://laracasts.com/series/php-for-beginners/episodes/7 and it came clear that what I have is an associative array, that has a key associated with a value. The only problem though, is that when the $key hits the variable from the controller inside of the view where im introducing the youtube link, the position doesn't move from 0.

Can I do a if statement that checks if the position is 0 or not?

If the $key == 0 && its the first resource? then do nothing, but if the $key > 0 && != first position, then $key++ or go to the next number. Is this a valid procedure?

Please or to participate in this conversation.