ghanem/themoviedb
Laravel package to seed TheMovieDB top-rated movies and genres into your database. Adds an Artisan command, optional queue support, configurable record count, and a /movies endpoint with customizable prefix/middleware. Supports scheduled runs via Laravel scheduler.
Installation
composer require ghanem/themoviedb
php artisan migrate
php artisan vendor:publish --provider="Ghanem\Themoviedb\ThemoviedbServiceProvider" --tag="config"
Configure API Key
.env:
THEMOVIEDB_KEY=your_api_key_here
THEMOVIEDB_NUM_OF_RECORDS=20 # Optional: Default is 100
First Use Case Seed top-rated movies via CLI:
php artisan themoviedb:seed top_rated_movies
Verify data via the /movies endpoint (default route).
Seeding Data
php artisan themoviedb:seed [type] # e.g., top_rated_movies, popular_movies
app/Console/Kernel.php):
$schedule->command('themoviedb:seed top_rated_movies')->daily();
Ensure cron runs php artisan schedule:run (e.g., * * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1).Queue-Based Processing
Enable async processing in .env:
THEMOVIEDB_ENABLE_QUEUE=true
php artisan queue:work).API Integration
use Ghanem\Themoviedb\Facades\Themoviedb;
$movies = Themoviedb::getMovies('top_rated');
config/themoviedb.php:
'prefix' => 'api/v1',
'middleware' => ['throttle:60'],
Data Validation
Movie model or use API responses directly:
$response = Themoviedb::callApi('movie/popular');
$movies = collect($response['results'])->map(fn($item) => [
'title' => $item['title'],
'vote_average' => $item['vote_average'],
]);
API Key Restrictions
GuzzleHttp\Exception\RequestException).Queue Configuration
THEMOVIEDB_ENABLE_QUEUE=true, ensure:
.env (e.g., QUEUE_CONNECTION=database).failed_jobs table exists (run php artisan queue:failed-table if missing).php artisan queue:work --once --verbose.Migration Conflicts
movies/genres tables.database/migrations/ for duplicates or customize the seeder to append data.Deprecated Endpoints
top_rated_movies, but TMDb’s API may change.try {
$response = Themoviedb::callApi('movie/popular');
} catch (\Exception $e) {
\Log::error('TMDb API Error:', ['response' => $response->getBody(), 'error' => $e->getMessage()]);
}
unique constraint to the movies table’s tmdb_id column.Custom Seeders
Override the seeder logic in app/Providers/ThemoviedbServiceProvider.php:
public function boot()
{
$this->app->extend('themoviedb.seeder', function ($seeder) {
return new CustomSeeder($seeder->getClient());
});
}
Additional Fields
Extend the Movie model to include TMDb-specific fields:
// app/Models/Movie.php
protected $casts = [
'release_date' => 'date',
'vote_average' => 'float',
];
Webhooks for Updates Use TMDb’s Change Log API to sync updates:
$changes = Themoviedb::callApi('changelog/movies');
// Process $changes['results'] to update your DB.
Testing Mock the API client in tests:
$mock = Mockery::mock('overload:Ghanem\Themoviedb\Themoviedb');
$mock->shouldReceive('callApi')->andReturn(['results' => []]);
How can I help you explore Laravel packages today?