2015-01-26 104 views
2

我在laravel上传了一个问题。我的观点:用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() }} 

我的控制器:

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'); 
} 

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

+0

做'dd(Input :: get('title'))'以确保它得到你认为它得到的东西。 – ceejayoz 2015-01-26 20:01:44

+0

当我尝试上传视频时出现问题 – 2015-01-26 20:05:12

+1

这是数据库错误。它告诉你,你正试图为'title'列保存空的值,这是不可空的。我会听听上面提出的建议ceejayoz。将'dd(Input :: get('title'))'放入'postCreate()'方法中,然后尝试上传视频。 – lesssugar 2015-01-26 21:32:52

回答

3

问题是,当你创建表/模式,你没有设置你的标题栏可以为空,

您可以通过两种方式解决这个问题。 你可以改变你的表模式,并设置标题栏为空,

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

或代替输入::获得(“标题”),您可以用默认值尝试这样即使它为空或null将有一个默认值。

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