Upload video with Laravel

6.3k Views Asked by At

I have a problem with my upload in laravel. My view :

{{ Form::open(array('url'=>'administration/video/create','method' => 'post','files'=>true)) }}
        <p>
            {{ Form::label('Titlu:') }}
            {{ Form::text('title',null,array('class'=>'form-control')) }}
        </p>
        <p>
            {{ Form::label('Description:') }}
            {{ Form::text('description',null,array('class'=>'form-control')) }}
        </p>
        <p>
            {{ Form::label('image','Imagine:') }}
            {{ Form::file('image','',array('id'=>'','class'=>'')) }}
        </p>
        <p>
            {{ Form::label('video','Video:') }}
            {{ Form::file('video','',array('id'=>'','class'=>'')) }}
        </p>
            {{ Form::submit('Add',array('class'=>'btn btn-primary')) }}
            {{ Form::reset('Reset',array('class'=>'btn btn-success')) }}
            {{ Form::close() }}

My controller:

public function postCreate(){
    $video = new \Video();
    $video->title       = Input::get('title');
    $video->description = Input::get('description');
    $video->video_image = '';
    $video->video_name  = '';
    if(Input::hasFile('image')) {
        $sImagePermalink = \Url_Lib::makePermalink($video->title);
        $image = Input::file('image');
        $filename = $sImagePermalink . "." . $image->getClientOriginalExtension();
        $path = public_path('content/video/' . $filename);
        Image::make($image->getRealPath())->resize(468, 249)->save($path);
        $video->video_image = 'content/video/' . $filename;

        $videoDocument = Input::file('video');
        $videofile = $sImagePermalink . "." . $videoDocument->getClientOriginalExtension();
        $pathVideo = public_path('content/video/' . $videofile);
        Input::file('video')->move('content/video/', $pathVideo);
        $video->video_name = 'content/video/' . $videofile;
    }

    $video->save();
    return Redirect::to('/administration/video/add/')
        ->with('message_succes','Video added');
}

I get an error : SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null I don't understand why? Please help me.

1

There are 1 best solutions below

0
serdar.sanri On

Issue is when you create table / schema, you didn't set your title column can be nullable,

You can solve it with two ways. you can either change your table schema and set the title column nullable,

Schema::create("yourtablename" , function($table){
 ........
  $table->string("title")->nullable();
 ........
});

or instead of Input::get("title") you can try with default value so even it is empty or null it will have a default value.

 Input::get("title" , "No title");