使用 Laravel 上传视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28157761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:46:39  来源:igfitidea点击:

Upload video with Laravel

phplaravellaravel-4laravel-3

提问by Harea Costea

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

我在 Laravel 中上传时遇到问题。我的看法 :

{{ 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.

我收到一个错误:SQLSTATE[23000]:完整性约束违规:1048 列“标题”不能为空我不明白为什么?请帮我。

回答by serdar.sanri

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") 你可以尝试使用默认值,这样即使它是空的或 null 它也会有一个默认值。

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