How to upload video in Laravel in the best way?

Lets go through a Laravel 8 upload video example. This example will help you upload video to database in Laravel 8. Lets create a basic example of Laravel 8 video upload with preview.

In this tutorial, we will create two routes one for get and another for the post method. we created a simple form with file input. So you have to simply select video and then it will upload in “video” directory of public folder of your Laravel project.

Step 1: Install Laravel 8 and Create new Project

The first step is to assure you work with Laravel 8 and create a new project with it.

composer create-project --prefer-dist laravel/upload_video blog

Step 2: Create new Routes

Next, we will add new two routes in routes/web.php file. One to generate input form and another for the post method.

Route::get('upload-video', [ UploadVideoController ::class, 'getUploadVideoForm' ])->name('video.upload');
Route::post('upload-video', [ UploadVideoController ::class, 'uploadVideo' ])->name('');

Step 3: Create new Controller

In the third step we will create a new UploadVideoController under the app/Http/Controllers directory, where we will define the two method uploadVideo() and getUploadVideoForm(). So one method handles get method another one for post.

namespace App\Http\Controllers;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class UploadVideoController extends Controller
    public function getUploadVideoForm()
        return view('upload-video');
    public function uploadVideo(Request $request)
        $this->validate($request, [
            'title' => 'required|string|max:255',
            'video' => 'required|file|mimetypes:video/mp4',
        $fileName = $request->video->getClientOriginalName();
        $filePath = 'videos/' . $fileName;
        $isFileUploaded = Storage::disk('public')->put($filePath, file_get_contents($request->video));
        // File URL to access the video in frontend
        $url = Storage::disk('public')->url($filePath);
        if ($isFileUploaded) {
            $video = new Video();
            $video->title = $request->title;
            $video->path = $filePath;
            return back()
            ->with('success','Video has been successfully uploaded.');
        return back()
            ->with('error','Unexpected error occured');

The getUploadVideoForm() method returns the view where a user can upload the video. Meanwhile, the uploadVideo() method deals with the uploaded video from the view. It validates the video type, checks the mimes types and its maximum size. Then stores the video to the public path specified for the project. The video URL is saved to access the video in frontend. A success message returns with the image path, if the process goes smoothly without errors.

Step 4: Create new blade file

The blade view that is returned in the getUploadVideoForm() method is coded as shown below.

<!DOCTYPE html>
      <title>Laravel Video Upload Form -</title>
      <link rel="stylesheet" href="">
      <div class="container mt-5">
         <div class="panel panel-primary">
            <div class="panel-heading">
               <h2>Laravel Video Upload Form-</h2>
            <div class="panel-body">
               @if ($message = Session::get('success'))
                   <div class="alert alert-success alert-block">
                      <button type="button" class="close" data-dismiss="alert">×</button>
                      <strong>{{ $message }}</strong>
               @if (count($errors) > 0)
               <div class="alert alert-danger">
                  <strong>Whoops!</strong> There were some problems with your input.
                     @foreach ($errors->all() as $error)
                     <li>{{ $error }}</li>
               <form action="{{ route('') }}" method="POST" enctype="multipart/form-data">
                  <div class="row">
                     <div class="col-md-12">
                        <div class="col-md-6 form-group">
                           <input type="text" name="title" class="form-control"/>
                        <div class="col-md-6 form-group">
                           <label>Select Video:</label>
                           <input type="file" name="video" class="form-control"/>
                        <div class="col-md-6 form-group">
                           <button type="submit" class="btn btn-success">Save</button>

The blade file above views the upload video option to the user. In addition, it has two separate divs that pop up required messages. One for success and one for error. The error directive will display any error messages.

Video MIME types details

The following mime types can be added to validate for a specific video formats.

Video TypeExtensionMime Type
3GP Mobile.3gpvideo/3gpp
Windows Media.wmvvideo/x-ms-wmv
iPhone Segment.tsvideo/MP2T
iPhone Index.m3u8application/x-mpegURL

Leave a Comment