# Laravel Videos
Attach videos from popular hosting platforms to Eloquent models.
Laravel Videos provides a simple and consistent API for associating external videos with your Eloquent models. It automatically detects the video provider, extracts metadata, and stores everything in a single `videos` table.
## Features
- Supports Laravel 11 and 12
- Automatic package discovery
- Polymorphic Eloquent relationship
- Video collections
- Automatic provider detection
- Stores video metadata
- Extensible provider architecture
### Built-in providers
- YouTube
- Vimeo
- VK Video
- Rutube
- Dailymotion
## Installation
Install the package via Composer:
```bash
composer require vhar/laravel-videos
```
Run the migrations:
```bash
php artisan migrate
```
## Usage
### Add the trait
```php
use Vhar\LaravelVideos\Traits\HasVideos;
class Post extends Model
{
use HasVideos;
}
```
### Add a video
```php
$post->videos()->createFromUrl(
'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
);
```
Or specify a collection:
```php
$post->videos()->createFromUrl(
'https://vimeo.com/76979871',
'intro'
);
```
### Retrieve videos
Get all videos:
```php
$post->videos()->get();
```
Get videos from a collection:
```php
$post->videos('intro')->get();
```
Get the first video:
```php
$post->video();
```
Get the first video from a collection:
```php
$post->video('intro');
```
## Video model
Each video stores the following information:
| Field | Description |
|--------|-------------|
| provider | Video provider name |
| url | Original video URL |
| video_id | External video identifier |
| embed_url | Video embed URL |
| title | Video title |
| thumbnail | Thumbnail URL |
| duration | Duration in seconds |
| collection | Optional collection name |
## Supported URLs
### YouTube
```
https://www.youtube.com/watch?v=...
https://youtu.be/...
https://www.youtube.com/shorts/...
```
### Vimeo
```
https://vimeo.com/...
https://player.vimeo.com/video/...
```
### VK Video
```
https://vkvideo.ru/video...
https://vk.com/video...
```
### Rutube
```
https://rutube.ru/video/...
https://rutube.ru/play/embed/...
```
### Dailymotion
```
https://www.dailymotion.com/video/...
https://dai.ly/...
```
## Custom providers
Create a class implementing the `VideoProvider` contract:
```php
use Vhar\LaravelVideos\Contracts\VideoProvider;
class CustomProvider implements VideoProvider
{
// ...
}
```
Register it in the configuration file:
```php
'providers' => [
\App\Videos\CustomProvider::class,
],
```
## License
The MIT License (MIT).