spAo's avatar
Level 1

Uploading video to table

Hello all! I'm trying to upload video file to table. I'm using resumable.js and laravel chunk. Video upload works fine it's uploads to lessons folder but now i want to store this video to table so user can upload lesson.

public function storeLesson(Request $request)
    {
        $request->validate([  // Added to store file to lessons table
            'name' => 'required',
            'file' => 'required',
            'durability' => 'required',
            'description' => 'required',
            'lesson_folder_id' => 'required',
      
        ]);
        
        $data = $request->all();  // Added to store file to lessons table
        
        
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
    
        if (!$receiver->isUploaded()) {
            // file not uploaded
        }

        $fileReceived = $receiver->receive(); // receive file
        if ($fileReceived->isFinished()) { // file uploading is complete / all chunks are uploaded
            $file = $fileReceived->getFile(); // get file
            $extension = $file->getClientOriginalExtension();
            $fileName = str_replace('.'.$extension, '', $file->getClientOriginalName()); //file name without extenstion
            $fileName .= '_' . md5(time()) . '.' . $extension; // a unique file name
            $disk = \Storage::disk('public');
            $path = $disk->putFileAs('lessons', $file, $fileName);

            $data['file'] = "$file"; // Added to store file to lessons table
    
            // delete chunked file
            unlink($file->getPathname());
            return [
                'path' => \Storage::url($path),
                'filename' => $fileName
            ];
        }
        // otherwise return percentage information
        $handler = $fileReceived->handler();
        return [
            'done' => $handler->getPercentageDone(),
            'status' => true
        ];


        Lesson::create($data);  // Added to store file to lessons table

    }

I added to my controller what is marked with " // Added to store file to lessons table " and now it's don't working so what's i'm doing wrong? When i receive video file then it must store to lessons table with other fields.

0 likes
1 reply
SilenceBringer's avatar

@spao do you know what return statement means? Function returns result and do not continue the work. You return results before saving the data to database

You method stopped here

            return [
                'path' => \Storage::url($path),
                'filename' => $fileName
            ];

or here

        return [
            'done' => $handler->getPercentageDone(),
            'status' => true
        ];

so, lesson creation

        Lesson::create($data);  // Added to store file to lessons table

never called. You need to store lesson before returning result

Please or to participate in this conversation.