Question By
Unsolved

Laravel video file uploading?

masumrahmanhasan ・

May 3rd, 2021 04:10 AM

what is the best way to upload video files in laravel.. If anyone know this pls let me know and pls give me the resource of that work

bobbyiliev ・

Aug 24th, 2022 01:24 AM

Hello,

Here is a quick way of doing this:

  • First create a migration for your videos table:
php artisan make:migration create_videos_table --create=videos
  • Then depending on your needs specify the columsn in the up method as follows:
  public function up()
  {
    Schema::create('videos', function (Blueprint $table) {
      $table->bigIncrements('id');
      $table->string('title');
      $table->string('video');
      $table->timestamps();
    });
  }
  • Next create your Video model:
php artisan make:model Video
  • In the model specify the filable property with the column that you've defined in your migration:
  protected $table = 'videos';
  protected $fillable = [
      'title', 'video'
  ];
  • After that create a video controller:
php artisan make:controller VideoController
  • Add a new method called upload:
   public function uploadVideo(Request $request)
   {
      $this->validate($request, [
         'title' => 'required|string|max:255',
         'video' => 'required|file|mimetypes:video/mp4',
   ]);
   $video = new Video;
   $video->title = $request->title;
   if ($request->hasFile('video'))
   {
     $path = $request->file('video')->store('videos', ['disk' =>      'public']);
    $video->video = $path;
   }
   $video->save();
   
  }
  • Then add a route for that upload method:
Route::post('video-upload', 'VideoController@upload')->name('videos.upload');
  • Finally create a form in your blade template:
<div>
<h3>Upload a video</h3>
<hr>
<form method="POST" action="{{ route('videos.upload') }}" enctype="multipart/form-data" >
{{ csrf_field() }}
<div >
<label>Title</label>
<input type="text" name="title" placeholder="Video Title">
</div>
<div >
<label>Select Video</label>
<input type="file"  name="video">
</div>
<hr>
<button type="submit" >Submit</button>
</form>

Additionally you could also use S3 to store your video files rather than uploading them to the local filesystem of your server.

Best,

Bobby