@warpig sorry but I think I'm lost what is the real question here. Based on your current setup, can you explain in simple terms the problem and what do you need?
Display 1 video from a collection inside a view
I want to be able to pass the location of a resource, it could be 0, 1, 2, 3, store it inside an href and check it against a foreach loop.
0 => array:8 [▶]
1 => array:6 [▶]
2 => array:6 [▶]
3 => array:6 [▶]
The foreach loop will most likely have 1 value, a dynamic youtube url with the ID as the dynamic content. But I can't wrap my head around the concept, it might be simple for some. I have a Controller with a function and im attempting to fetch this array like this:
public function show()
{
$thps4Videos = Http::get('http://thvid-api.herokuapp.com/videos/game/THPS4')->json();
dd($thps4Videos);
return view ('videos.show', compact('thps4Videos'));
}
So anytime this gets a request I will only get the column for the "id" value, I'd have to associate it with the location from the collection array, and display it on this view, videos.show by associating the position and the value of the column id.
The goal is to display 1 video for each array from the collection in 1 page.
This is one example of 1 array:
0 => array:8 [▼
"_id" => "605a43ea667cd39060f435ac"
"Title" => "THPS4 Rewind: Summer Of 2003 Unreleased (Edited by Maxfli)"
"Game" => "THPS4"
"ID" => "Qxd7D1KN1No"
"Thumbnail" => "https://i.ytimg.com/vi_webp/Qxd7D1KN1No/maxresdefault.webp"
"newduration" => "00:22:53"
"category" => "gameplay"
"updatedAt" => "2021-04-12T17:45:27.167Z"
]
The column that can bring me to the video is ID, so the dynamic URL would have to be something like this:
https://www.youtube.com/watch?v={{ $thps4Videos['ID'] }}
´´´
@warpig but my point here is, just change $thps4['ID'] to $key instead.
@forelse ($thps4Results as $key => $thps4)
<div wire:key="results-{{ $thps4['_id'] }}">
<img
src="{{ $thps4['Thumbnail'] }}">
<div>
<a href="play/{{ $key }}">
<svg>
</svg>
</a>
<p>
{{ $thps4['Title'] }}
</p>
</div>
</div>
@empty
@foreach (range(1,6) as $thumbnail)
<div>
<p>loading</p>
</div>
@endforeach
@endforelse
so in your /play/{$id} you can show the video like this:
public function show($id)
{
$thps4Videos = Http::get('http://thvid-api.herokuapp.com/videos/game/THPS4/1/21')->json();
$thps4Video = $thps4Videos[$id]; // which is an index
return view ('videos.show', compact('thps4Video'));
}
and actually, I just notice that http://thvid-api.herokuapp.com/videos/game/THPS4/1/21 is modifiable using an index and how many records, so you can even get better performance using $key like this:
public function show($id)
{
$id++;
$thps4Videos = Http::get('http://thvid-api.herokuapp.com/videos/game/THPS4/$id/1')->json();
$thps4Video = $thps4Videos[0]; // get the first one
return view ('videos.show', compact('thps4Video'));
}
so you don't need to load ALL
Please or to participate in this conversation.