Tutorials · 5 min read

Image of upload form showing available fields

Upload a video with Laravel

Create a form and upload a video to api.video using Laravel

Erikka Innes

October 13, 2021

The first step to displaying your video online is getting it uploaded. Today we'll go over how to upload a video using Laravel 8 with api.video's PHP Client. Before we dive in, let's go over what we'll be doing when we upload a video.

Uploading a video is a two step process. First, we create a container for the video and retrieve the video ID. Then, we upload a video into the container by referencing the ID. You can only upload a video to a container once, but you can modify details about the video at any time after that. Our uploader will let you add a title, description and list whether you want to have the video be public or private and whether you want to offer an MP4 link of the video for download.

What you'll need

To get started, make sure you have these things:

You can also review the API reference documentation for the endpoints used in this project:

Create your project

The first thing you'll need is a Laravel 8 project to work with. Here's what I did:

  1. In a terminal make a new project by typing: laravel new fileuploader or similar project name.
  2. Open your project folder, you'll need to install a few things.
  3. You'll need to add the api.video PHP client, so at the terminal type: composer require api-video/php-api-client
  4. You'll also need the symfony http client, at the terminal type: composer require symfony/http-client
  5. And the last thing to add is nyholm/psr7: composer require nyholm/psr7

You can type composer update to make sure everything is pulled in to your project and configured.

Create a controller

We will need a controller for our video uploader, so let's go ahead and do that now. In the terminal in your project folder type:

php artisan make:controller UploadController

This will automatically create what we need to start building the controller.

Set up the home page with an upload form

For this walkthrough, we're using the home page to create an upload form. So in your views folder, go into home.blade.php and create the form. I selected to allow the user to add basic details about their project. I'm also using bootstrap, so the basic form looks like this:

Image of upload form showing available fields

And here is the code that creates this form:

php

We pull in the bootstrap details up top under the Scripts and Styles sections. Then for our form, we have all the form data sent in a POST request to the '/upload' route. (We'll need to create that route in a moment.)

The form is straightforward - we list the type of data as text for the name of the file and the description of the file. For choosing whether the file will be public or offer an MP4 for download, we use a radio button, so the type is radio. For the video, we choose type file and then we set up our submit button. Bootstrap styles everything for us, so we're all set here. Now we need to handle the route.

Set up the /upload route

Open your web.php file and add a new route:

Route::post('/upload', 'UploadController@upload');

This route indicates that when submit is clicked in the form, it should go to /upload and carry out the upload instructions listed for the UploadController.

We don't have that set up yet, so let's do that next. Also, the other routes are added into the project, you can see everything if you download the repository.

Set up your UploadController

Our controller takes the details provided by the form, then uploads the content to api.video. Behind the scenes, we're going to need our api.video API key, so let's make sure we handle it securely.

  1. Go to your .env file. In the top section, you'll see items to do with your app. At the bottom of the list of items beginning APP_ you'll see APP_APIVIDEO=yourapikey. Replace yourapikey with your key (retrieve it from your api.video dashboard). Don't add quotes.
  2. Open RouteServiceProvider.php.
  3. In the file, take the commenting \ off of the line protected $namespace = 'App\Http\Controllers';. This returns the use of the automatic namespace, which is handy.
  4. Open UploadController.php.
  5. Add this code:

php

In our code, we set up the api.video client and collect the information from the request object that comes in from the form. We check if the data for the radio buttons is set and if it is, we add it to our payload for upload. Then we send the data and redirect the user to a page that says 'Success!'

Conclusion

These are the basics for uploading a video to api.video. You can try using different styles or set ups if you want to allow users to add more metadata on upload. If you have any questions or suggestions, please share them on our community forum. Thanks for reading!

Try out more than 80 features for free

Access all the features for as long as you need.
No commitment or credit card required

Video API, simplified

Fully customizable API to manage everything video. From encoding to delivery, in minutes.

Built for Speed

The fastest video encoding platform. Serve your users globally with 140+ points of presence. 

Let end-users upload videos

Finally, an API that allows your end-users to upload videos and start live streams in a few clicks.

Affordable

Volume discounts and usage-based pricing to ensure you don’t exceed your budget.