Hello dev, Today we are going to learn how to fetch data from TMDb API in Laravel 9. This tutorial will cover on how to call TMDb API in laravel 9 application to fetch records.
Proper and secure way to call TMDb API in laravel 9 application. We are going to learn how to get movies, tv shows, persons, etc. with the help of TMDb API. You may also check our tutorial on How to Call External API in Laravel? (2022)
About TMDb – The Movie Database (TMDb) is a community built movie and TV database. Every piece of data has been added by our amazing community dating back to 2008. TMDb’s strong international focus and breadth of data is largely unmatched and something we’re incredibly proud of.
This tutorial going to work with laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 application. Please test and let us know in the comment section.
Also Read: Add Toast Notification in Laravel 9
Steps on Using TMDb API in Laravel 9
- Step 1: Install Laravel 9 application
- Step 2: Get TMDb API Key
- Step 3: Configure .env file
- Step 4: Configure service file
- Step 5: Create Routes
- Step 6: Create Demo Controller File
- Step 7: Create Demo Blade View File
- Step 8: Testing
Step 1: Install Laravel 9 application
First, we are going to install a fresh new laravel 9 application into our system. To install a laravel application run the following code in terminal.
composer create-project laravel/laravel tmdb-api-app
cd tmdb-api-app
Or, you may create new Laravel projects by globally installing the Laravel installer via Composer:
composer global require laravel/installer
laravel new example-app

Also Read: Laravel 9 Shopping Cart Tutorial and Example
Step 2: Get TMDb API Key
Now we are going to get TMDb API key. To get the API Key Register a account in themoviedb.org.
TMDb offers a powerful API service that is free to use as long as you properly attribute us as the source of the data and/or images you use. You can find documentation at developers.themoviedb.org.
Now follow the bellow image to get the TMDb v3 or v4 API Key.

In this example we are using TMDb v4 API Key. So copy the code and we are going to use it in our next step.
Step 3: Configure .env file
Next step, We are going to edit our .env file to add the endpoint and TMDb API Key to our environment.
Open .env file and add the following code.
.env
TMDB_ENDPOINT=https://api.themoviedb.org/3/
TMDB_APP_KEY=ADD_TMDB_API_HERE

TMDB_ENDPOINT – https://api.themoviedb.org/3/ is going to be our api endpoint. So that we didn’t have to repeat the ENDPOINT again and again for movies, tv shows, persons etc.
TMDB_APP_KEY is the API key which we copy from the above step just change ADD_TMDB_API_HERE with the once which we copy earlier.
Also Read: Laravel Add Watermark on Images
Step 4: Configure service file
Now we are going to edit the services.php file. Add the following code to config/services.php file.
config/services.php
'tmdb' => [
'endpoint' => env('TMDB_ENDPOINT'),
'api' => env('TMDB_APP_KEY'),
],

Step 5: Create Routes
We are going to create a routes for our application. Open routes/web.php and add the following routes for our application.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/demo', [DemoController::class, 'demo'])->name('demo');

Also Read: How to Check Date is Today’s Date or not in Laravel Carbon?
Step 6: Create Demo Controller File
Now we are going to create Demo Controller for retrieve data from TMDb and to send fetch data to View Blade file.
Create a DemoController.php file inside app\Http\Controllers and add the following code.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
class DemoController extends Controller
{
public function demo(){
$tmdb_id = 436270; //Black Adam (2022) Movie TMDB ID
$data = Http::asJson()
->get(config('services.tmdb.endpoint').'movie/'.$tmdb_id. '?api_key='.config('services.tmdb.api'));
return view('demo',compact('data'));
}
}

Also Read: How to Get All env Variables in Laravel?
Step 7: Create Demo Blade View File
Now we will create a blade view file to display our data. Create a demo.blade.php inside resources/views and enter the following code inside it.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Working with TMDb API in Laravel 9 - LaravelTuts</title>
<!-- Fonts -->
<link href="https://fonts.bunny.net/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
<!-- TailwindCss CDN -->
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="antialiased">
<div class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center py-4 sm:pt-0">
<div class="max-w-6xl mx-auto sm:px-6 lg:px-8">
<div class="mt-8 bg-white dark:bg-gray-800 overflow-hidden shadow sm:rounded-lg">
<div class="flex items-center p-4 w-[920px]">
<div class="w-3/12">
<img src="https://www.themoviedb.org/t/p/w220_and_h330_face{{ $data['poster_path'] }}" alt="Poster" class="rounded ">
</div>
<div class="w-9/12">
<div class="ml-5">
<h2 class="text-2xl text-gray-900 font-semibold mb-2">{{ $data['title'] }} ({{ date('Y',strtotime($data['release_date'])) }})</h2>
<div class="mb-1 flex mb-4 sm:flex-nowrap flex-wrap">
@if(count($data['genres']) > 0)
@php
$num_of_items = count($data['genres']);
$num_count = 0;
@endphp
@foreach ($data['genres'] as $singleGenre)
<span class="text-sm">
{{ $singleGenre['name'] }}
</span>
@php
$num_count = $num_count + 1;
if ($num_count < $num_of_items) {
echo '<span class="mx-2 flex items-center">•</span>';
}
@endphp
@endforeach
@endif
</div>
<div class="flex items-center space-x-2 tracking-wide pb-1">
<h1 class="text-gray-500">Release Date</h1>
<p class="leading-6 text-sm">{{ $data['release_date'] }}</p>
</div>
<div class="flex items-center space-x-2 tracking-wide pb-1">
<h1 class="text-gray-500">Rating</h1>
<p class="leading-6 text-sm">{{ $data['vote_average'] }}</p>
</div>
<div class="flex items-center space-x-2 tracking-wide pb-1">
<h1 class="text-gray-500">Duration</h1>
<p class="leading-6 text-sm">{{ $data['runtime'] }} min</p>
</div>
<p class="leading-6 mt-5 text-gray-500">{{ $data['overview'] }}</p>
</div>
</div>
</div>
</div>
<div class="text-center text-sm text-gray-500 sm:text-center my-5">
Tutorial By
<a href="https://laraveltuts.com" class="ml-1 underline">
LaravelTuts.com
</a>
</div>
</div>
</div>
</body>
</html>

Also Read: Laravel Pagination Tutorial
Step 8: Testing
Now everything is ready we are going to test our application. Run the following command in terminal to start the laravel serve.
php artisan serve
and the open the following link any web browser.
http://127.0.0.1:8000/demo
Preview:

Conclusion
Today, We had learn Working with TMDB API in Laravel 9. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.