inzmam's avatar

Unable to upload video file

When i am going to upload a video file i m getting an error as "SQLSTATE[HY000]: General error: 1364 Field 'filename' doesn't have a default value (SQL: insert into video_stores (updated_at, created_at) values (2018-03-16 04:14:58, 2018-03-16 04:14:58))".

I don't why this error is being thrown, however i am passing all the values. Please help me in this case.

HomeController:- 'public function uploadVideo(Request $request) { $vid=new VideoStore; if($request->hasFile('Upload_video')) { $video=$request->file('Upload_video'); $filename=time().'.'.$video->getClientOriginalExtension(); $location=public_path('video/'.$filename);

        //Image::make($video)->resize(400,200)->save($location);
       
        $vid->move($location,$filename);
        $vid->filename='inzmam';
         $vid->path='ul haq';
        
    }
    $vid->save();
    Session::flash('success','Your video has been uploaded');
    return redirect()->route('pages.index');
}'

Migration:-

'public function up() { Schema::create('video_stores', function (Blueprint $table) { $table->increments('id'); $table->string('filename'); $table->string('path'); $table->timestamps(); }); }

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('video_stores');
}'.
0 likes
24 replies
arthurvillar's avatar

This error is telling you exactly what is happening: the if($request->hasFile('Upload_video') returns false, so all the logic within it never runs. The next line that runs is $vid->save(), which is adding only the created_at and updated_at. Laravel adds those automatically behind the scenes every time you insert anything new into your table. This means there is no file coming from your form.

Looks like you don't have enctype="multipart/form-data" in your form tag on the view file. If you don't have that the file will never reach the controller. Your form tag should look like this

<form method="POST" action="/your-route-here" enctype="multipart/form-data">
inzmam's avatar

Here is my form

' {{Form::open(['route'=>'pages.uploadVideo','files'=>true])}} {{Form::file('Upload_video',['style'=>'margin-top:10px;'])}} {{Form::submit('Upload Video',['class'=>'btn btn-default','style'=>'margin-top:10px;'])}} {{Form::close()}} '

arthurvillar's avatar

Try creating the form without using these helpers from Laravel. Create the actual tag with correct attributes and inputs. It will look like this

<form method="POST" action="/your-route-here" enctype="multipart/form-data">
    <input type="file" name="Upload_video">
    <input type="submit" value="Upload Video" name="submit">
</form>

ps: please use the proper format to post code here, it is very difficult to read it otherwise.

inzmam's avatar

Interestingly, i have used FORM facade only to upload images which was uploaded successfully and stored in database table also. Just wanted to ask you if below code is causing an error,

//Image::make($video)->resize(400,200)->save($location);

    $vid->move($location,$filename);
    $vid->filename=$filename;
     $vid->path=$video;
    
arthurvillar's avatar

This code is never being reached, so you can't know if it causes errors or not. But by looking at it, I wonder where does the method move() comes from. Looks like $vid is an instance of your VideoStore class, so do you have that method created in there? If not this will fail for sure.

inzmam's avatar

$vid is an object of VideoStores model and move function is being used to store the video in video folder under public, do we have any other way out to upload video files and why can't it be done on the same way as i did it to upload images??

Snapey's avatar

just approach it systematically.

dd($vid) and see what it contains


        $vid->move($location,$filename);
        $vid->filename='inzmam';
         $vid->path='ul haq';
    
    dd($vid);
        
    }
    $vid->save();

inzmam's avatar

dd($vid) does not contain anything, however when i upload images instead of video it has all the info.

does laravel support video upload or it needs different configuration. I think it needs some different config.

please help me in this case. I am stuck in this since a long time

inzmam's avatar

Someone please help me.......

Snapey's avatar

video is just a file

dd($vid) does not contain anything

Well it should, it should at least be an instance of your VideoStore model?

inzmam's avatar

thanks for your reply! Yeah it does, meant anything of the uploaded video, but when i upload images, it shows all the info??

Snapey's avatar

this is a two way thing. Dont short change your replies. things like doesnt work and doesnt contain anything are no help.

So, by hitting the dd, we have established that your code thinks there is an uploaded file.

what I cannot understand is if your previous lines are

$vid->filename='inzmam';
$vid->path='ul haq';

why would $vid be empty?

inzmam's avatar

I am sorry, if it has caused confusion.

$vid->filename='inzmam';
$vid->path='ul haq';

was just written for debugging purpose. However, I have just noticed that my code execution does not enter inside the @if code block.

The actual data that i want to pass into the database table is

 $vid->filename=$filename;
             $vid->path=$path;
inzmam's avatar

The video size is just 7MB, i have also configured the php.ini file to increase the max file size.

Snapey's avatar

oh for flip sake

The point of putting the dd INSIDE the if section was to see if you were detecting the uploaded file

so no then. I suggest you go back and look at earlier answers again

Snapey's avatar

start with basics.

check what $request contains

inzmam's avatar

Yeah i have tried previous answers also, when i am keeping this code

 $vid->filename='inzmam';
 $vid->path='ul haq';

inside the if section to check if this string is printed, then also it does not print it. What i want to bring it to your notice is, my if section itself is not being executed at all. What could be possible reasons for this.

Snapey's avatar

start with basics.

check what $request contains

most likely issue is that your upload field is not called Upload_video

inzmam's avatar

Yeah same i have tried to check if at least it contains the name of the file, then it does not contain the name also because if section is not being executed.

I am surprised when i am uploading the images instead videos, it contains all the details of images but it does not contain the video details while uploading the same.

Snapey's avatar

start with basics.

check what $request contains

inzmam's avatar

Keeping

 $video=$request->file('Upload_video');
 dd($video);

outside the if section, it shows me the file details but when keep the same code inside the if block it does not. Just wanted to know from you what are the possible reasons for @if section not being excuted.

inzmam's avatar

thanks for your reply @Snapey . Issue is fixed now. When i use has() instead of hasFile() it finds the file, after that there was an issue of file size which i fixed it by modifying the correct php.ini file.

Thanks to all of you who have been with me for so long.

Please or to participate in this conversation.