Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

darkzone's avatar

Youtube API data foreach

Hello,

I need some help with Youtube API data optimization. In my app, I'm getting YT videos data by stored video id in my database. But there's a problem with longer site loading than usual.

Using alaouy/youtube package.

@foreach($videos as $video)

    @php

        $jsonData = Youtube::getVideoInfo($video->youtube_video_id);
        $videoDuration = $jsonData->contentDetails->duration;
        $duration = new DateInterval($videoDuration);
        $channelTitle = $jsonData->snippet->channelTitle;
        $channelId = $jsonData->snippet->channelId;
        $views = $jsonData->statistics->viewCount;
        $description = $jsonData->snippet->description;

    @endphp

    <div>
        // SOME BLADE CODE
    </div>

@endforeach

Is it possible to make it other way?

0 likes
6 replies
Abdelhammied's avatar

well can you be more prespective , is Youtube your model name or the used API ?

if any of them why are you looping then calling the data in each load this is the reason of this slowly , as better approach you should get all videos from your model / API then loop at them one time ..

darkzone's avatar

@abdelhammied, I have a Video model where I have stored youtube_video_id of each video. I looping them through foreach and getting video data through the Youtube API (using alaouy/youtube package).

hollyit's avatar

Calling to the YouTube api like that is going to slow things down. You also risk rate limits and getting blocked. You should cache everything from the API and refresh it every so often. Even if you can live with the delay in rendering that page, if you are putting this on a live site, then you will face huge problems down the road.

siangboon's avatar

i think you should get multiple videos at one request instead of loop and query every videos...

$videoList = Youtube::getVideoInfo(['rie-hPVJ7Sw','iKHTawgyKWQ']);
darkzone's avatar

I've tried make caching, but I can't do this. I've never do this.

This is my index function. How to loop videos ids to this getVideoInfo function?

$seconds = Carbon::now()->addDay();

        $videos = Cache::remember('videos', $seconds, function () {
            $apiData = Youtube::getVideoInfo(function() {
                // foreach?
            });
            return $apiData ;
        });


        return view('videos.index', compact('videos'));
darkzone's avatar
darkzone
OP
Best Answer
Level 1

Okay, I've done this thing.

 public function index() {

        $videos = Video::latest('created_at')->paginate(12);

        $seconds = Carbon::now()->addDays(7);

        foreach($videos as $video) {
            $id = $video->id;
            $apiData = Cache::remember('youtube.video.' . $id, $seconds, function() use ($id) {
                $video = Video::find($id);
                $apiData = Youtube::getLocalizedVideoInfo($video->youtube_video_id, 'pl');

                return $apiData;
            });
        }

        return view('videos.index', compact('videos', 'apiData'));
    }

    public function show($slug) {

        $seconds = Carbon::now()->addDay();
        $video = Video::where('slug', $slug)->firstOrFail();

        $apiData = Cache::remember('youtube.video.' . $video->id, $seconds, function() use ($slug) {
            $video = Video::where('slug', $slug)->firstOrFail();
            $apiData = Youtube::getLocalizedVideoInfo($video->youtube_video_id, 'pl');

            return $apiData;
        });

        // Catch deleted video exception
        if (empty($apiData)) {
            abort(404);
        }

        $channelId = $apiData->snippet->channelId;
        $channelJson = Cache::rememberForever('youtube.channel.' . $channelId, function () use ($channelId) {
            $channelJson = Youtube::getChannelById($channelId);
            return $channelJson;
        });

        return view('videos.show', compact('apiData', 'video', 'channelJson'));
    }

Please or to participate in this conversation.