PHPackages                             anandukrishnakk/laravel-youtube-trending - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. anandukrishnakk/laravel-youtube-trending

ActiveLibrary[API Development](/categories/api)

anandukrishnakk/laravel-youtube-trending
========================================

Fetch and list trending YouTube videos and shorts with region support and caching.

100PHPCI failing

Since Jun 1Pushed 1mo agoCompare

[ Source](https://github.com/anandukrishnakk/laravel-youtube-trending)[ Packagist](https://packagist.org/packages/anandukrishnakk/laravel-youtube-trending)[ RSS](/packages/anandukrishnakk-laravel-youtube-trending/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel YouTube Trending Package
================================

[](#laravel-youtube-trending-package)

[![Latest Version on Packagist](https://camo.githubusercontent.com/720d5cd24dfd8b8955f6f01593c0ae8b4c222a34b980d8a6fbe953de8aaf5686/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7061636b61676973742d76312e302e302d626c75652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/anandukrishnakk/laravel-youtube-trending)[![Total Downloads](https://camo.githubusercontent.com/f51dbf443b0ed3234ceda035a8b676a08cf8a47b2f33b32e25f3b55bfccc4f33/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f776e6c6f6164732d302d677265656e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/anandukrishnakk/laravel-youtube-trending)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

A high-performance, developer-friendly Laravel package to list trending YouTube videos and YouTube Shorts. It supports dynamic region/location changing, smart DTO normalization, and robust caching to respect your YouTube API quota.

---

Key Features
------------

[](#key-features)

- 🌍 **Dynamic Location Switching**: Fetch trending lists for any country (ISO 3166-1 alpha-2, e.g., US, IN, GB, JP).
- ⚡ **Highly Optimized Caching**: Caches results (defaults to 1 hour) to protect your YouTube API limits (reduces query cost from 100 quota units to 0).
- 🎬 **Hybrid YouTube Shorts Algorithm**: Since YouTube has no native API for trending Shorts, this package uses a unique quota-friendly heuristic that filters trending lists by duration ($\\le 60$s) and falls back to a smart hashtag query when needed.
- 📦 **Rich DTO Parsing**: Raw JSON payloads are mapped into clean `TrendingVideo` PHP objects with handy helpers like `.getVideoUrl()`, `.getDurationInSeconds()`, and `.getEmbedUrl()`.

---

Installation
------------

[](#installation)

### 1. Install via Composer

[](#1-install-via-composer)

To install this local package in your current Laravel application, add a path repository definition to your application's `composer.json` first:

```
"repositories": [
    {
        "type": "path",
        "url": "../y-pack"
    }
],
```

Then, run:

```
composer require anandukrishnakk/laravel-youtube-trending
```

### 2. Publish Configuration

[](#2-publish-configuration)

Publish the config file into your project:

```
php artisan vendor:publish --tag="youtube-trending-config"
```

This will create a `config/youtube-trending.php` configuration file.

### 3. Add API Credentials

[](#3-add-api-credentials)

Obtain a YouTube Data API v3 key from the [Google Cloud Console](https://console.cloud.google.com/) and add it to your `.env` file:

```
YOUTUBE_API_KEY=your_actual_youtube_data_api_v3_key
YOUTUBE_TRENDING_REGION=US
```

---

Configuration File
------------------

[](#configuration-file)

The published config file (`config/youtube-trending.php`) lets you control the behavior:

```
return [
    // YouTube Data API Key
    'api_key' => env('YOUTUBE_API_KEY', ''),

    // Default Region (e.g. 'US', 'GB', 'IN', 'JP')
    'default_region' => env('YOUTUBE_TRENDING_REGION', 'US'),

    // Cache settings to save your daily 10k quota
    'cache' => [
        'enabled' => env('YOUTUBE_TRENDING_CACHE_ENABLED', true),
        'ttl' => env('YOUTUBE_TRENDING_CACHE_TTL', 3600), // 1 hour
        'prefix' => 'youtube_trending_',
    ],

    // Shorts fallback query setting
    'shorts' => [
        'fallback_search' => env('YOUTUBE_TRENDING_SHORTS_FALLBACK', true),
        'fallback_query' => '#Shorts',
    ],
];
```

---

Usage Guide
-----------

[](#usage-guide)

You can access the service using either the **`YoutubeTrending` Facade** or via **Dependency Injection**.

### 1. Fetching Trending Videos

[](#1-fetching-trending-videos)

Fetch standard popular trending videos in the default or custom regions.

```
use Anandukrishnakk\YoutubeTrending\Facades\YoutubeTrending;

// Get 15 trending videos in default region (US)
$videos = YoutubeTrending::limit(15)->getVideos();

// Get 10 trending videos for Great Britain (GB)
$gbVideos = YoutubeTrending::region('GB')->limit(10)->getVideos();

// Get 20 trending videos for India (IN)
$inVideos = YoutubeTrending::region('IN')->limit(20)->getVideos();
```

### 2. Fetching YouTube Shorts

[](#2-fetching-youtube-shorts)

Fetch trending YouTube Shorts (videos under 60 seconds).

```
use Anandukrishnakk\YoutubeTrending\Facades\YoutubeTrending;

// Get 10 trending Shorts in default region
$shorts = YoutubeTrending::limit(10)->getShorts();

// Get 15 trending Shorts for Japan (JP)
$jpShorts = YoutubeTrending::region('JP')->limit(15)->getShorts();
```

---

Output Structure (`TrendingVideo` DTO)
--------------------------------------

[](#output-structure-trendingvideo-dto)

Both `getVideos()` and `getShorts()` return a Laravel `Collection` of `TrendingVideo` DTOs. Here is what is available on each DTO:

Property / MethodTypeDescription`$video->id``string`The YouTube Video ID (e.g. `abc123xyz`).`$video->title``string`The title of the video.`$video->description``string`The description text.`$video->thumbnailUrl``string`Highest resolution thumbnail URL.`$video->publishedAt``string`Publication date in ISO 8601 format.`$video->viewCount``int|null`Number of views (optional/nullable).`$video->likeCount``int|null`Number of likes (optional/nullable).`$video->channelTitle``string`Name of the publishing channel.`$video->channelId``string`ID of the publishing channel.`$video->duration``string`YouTube duration format (e.g. `PT3M20S`).`$video->isShort``bool`True if the duration is 60 seconds or less.`$video->getVideoUrl()``string`URL to watch the video (automatically formats as `/shorts/ID` if isShort).`$video->getShortsUrl()``string`URL strictly in Shorts format: `https://youtube.com/shorts/ID`.`$video->getEmbedUrl()``string`URL for embeds: `https://youtube.com/embed/ID`.`$video->getDurationInSeconds()``int`Duration in seconds (e.g., `PT1M15S` =&gt; `75`).`$video->toArray()``array`Export all properties into a clean array.---

Front-end Integration Example (Tailwind CSS)
--------------------------------------------

[](#front-end-integration-example-tailwind-css)

Create a stunning responsive UI in your Blade files using the following structures:

### 📺 Video Grid

[](#-video-grid)

```

    @foreach($videos as $video)

                    {{ gmdate($video->getDurationInSeconds() >= 3600 ? 'H:i:s' : 'i:s', $video->getDurationInSeconds()) }}

                {{ $video->title }}
                {{ $video->channelTitle }}

                    {{ number_format($video->viewCount) }} views
                    •
                    {{ \Carbon\Carbon::parse($video->publishedAt)->diffForHumans() }}

                    Watch Video

    @endforeach

```

### 📱 Shorts Grid (Vertical Aspect Ratio)

[](#-shorts-grid-vertical-aspect-ratio)

```

    @foreach($shorts as $short)

                    {{ $short->title }}
                    {{ $short->channelTitle }}

                    @if($short->viewCount)
                        {{ number_format($short->viewCount) }} views
                    @endif

                        Watch Short

    @endforeach

```

---

Testing
-------

[](#testing)

Run unit and feature tests to verify execution:

```
./vendor/bin/phpunit
```

---

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT License](LICENSE.md).

###  Health Score

21

—

LowBetter than 17% of packages

Maintenance59

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/42e109b54ed72ea5cf22bbadefa1b8d686eaa6b0cbbd1144b7142721ea6e750b?d=identicon)[ananduk6](/maintainers/ananduk6)

---

Top Contributors

[![anandukrishnakk](https://avatars.githubusercontent.com/u/14835378?v=4)](https://github.com/anandukrishnakk "anandukrishnakk (2 commits)")

### Embed Badge

![Health badge](/badges/anandukrishnakk-laravel-youtube-trending/health.svg)

```
[![Health](https://phpackages.com/badges/anandukrishnakk-laravel-youtube-trending/health.svg)](https://phpackages.com/packages/anandukrishnakk-laravel-youtube-trending)
```

###  Alternatives

[exsyst/swagger

A php library to manipulate Swagger specifications

35916.4M7](/packages/exsyst-swagger)[hubspot/api-client

Hubspot API client

24016.2M20](/packages/hubspot-api-client)[pocketmine/bedrock-protocol

An implementation of the Minecraft: Bedrock Edition protocol in PHP

172445.0k18](/packages/pocketmine-bedrock-protocol)[botman/driver-telegram

Telegram driver for BotMan

93459.5k6](/packages/botman-driver-telegram)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
