【问题标题】:Laravel 5.1: can't upload a video fileLaravel 5.1:无法上传视频文件
【发布时间】:2017-12-28 21:09:35
【问题描述】:

提交文件时出现以下错误:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'video_ogg' cannot be null (SQL: insert into `profiles` (`about_me`, `video_ogg`, `updated_at`, `created_at`) values (lorem, , 2017-07-23 02:15:50, 2017-07-23 02:15:50))

其中指出字段 video_ogg cannot be null 但是当我在调试模式下验证时该字段不为空(见下文)

控制器

    public function store(Request $request)
   {
      // Validation //
      $validation = Validator::make($request->all(), [
         'about_me' => 'required',
         'video_ogg'    => 'required|mimes:mp4,ogx,oga,ogv,ogg,webm|min:1|max:3240',
      ]);

      // Check if it fails //
      if( $validation->fails() ){
         return redirect()->back()->withInput()
                          ->with('errors', $validation->errors() );
      }

      $profile = new Profile;

      //Debugging
      dd($request->files);

      // save media data into database //
      $profile->about_me = $request->input('about_me');
      $profile->video_ogg = $request->input('video_ogg');
      $profile->save();

      return redirect('/profile')->with('message','You just created your profile!');
   }

调试结果

FileBag {#45 ▼
  #parameters: array:1 [▼
    "video_ogg" => UploadedFile {#30 ▼
      -test: false
      -originalName: "mov_bbb.ogg"
      -mimeType: "audio/ogg"
      -size: 614492
      -error: 0
    }
  ]
}

正如您在调试视频时看到的那样,视频已上传,或者我的意思是在请求数组中,但我仍然有一条错误消息。

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'video_ogg' cannot be null (SQL: insert into `profiles` (`about_me`, `video_ogg`, `updated_at`, `created_at`) values (lorem, , 2017-07-23 02:15:50, 2017-07-23 02:15:50))

查看

{!! Form::open(['url'=>'/profile', 'method'=>'POST', 'files'=>'true']) !!}
...
      <div class="form-group">
         <label for="video_ogg">Upload Video (ogg)</label>
         <input type="file" class="form-control" name="video_ogg">
      </div>
...

哪个输出这个

<form method="POST" action="http://localhost:8000/profile" accept-charset="UTF-8" enctype="multipart/form-data"><input name="_token" type="hidden" value="KxvK6tONoUhBCx58ESlbE1hh9eP8hy5nQyNqb62W">

所以我确实验证了enctype="multipart/form-data" 在表单中。

型号

class Profile extends Model
{
    //
    protected $fillable = ['video_ogg', 'about_me'];
}

【问题讨论】:

  • 您是否将video_ogg 设置为可在您的模型上填充?
  • 你可以试试dd($request-&gt;input('video_ogg')) 并发布结果吗?
  • @VandolphReyes 它是空的
  • 怎么样。 $v_ogg = $request-&gt;input('video_ogg') dd(v_ogg-&gt;mimeType)
  • @VandolphReyes 试图获取非对象的属性

标签: php laravel-5 laravel-5.1 laravel-form


【解决方案1】:

我猜你应该使用file 方法而不是inputhttps://laravel.com/docs/5.4/requests#files

【讨论】:

    猜你喜欢
    • 2016-08-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2018-09-30
    相关资源
    最近更新 更多