Laravel文件上载名称必须是唯一的

时间:2016-11-07 10:56:55

标签: php mysql laravel laravel-5 laravel-5.2

正如标题所说,当用户上传文件名称时,我需要这是唯一的,如果不是,则抛出错误。

这是我的代码

$this->validate($request, [
    'file' => 'unique:files,name',
]);

但它仍然传递并将数据插入数据库。有没有人有想法?

编辑:

这是html表单

    {!! Form::open(array('url' => '/upload', 'files' => true)) !!}
    {!! Form::text('description','document description') !!}
    {!! Form::select('users', $users,null, ['placeholder' => 'Pick a user'])  !!}
    {!! Form::text('commentary','commentary') !!}
    {!! Form::select('cat',$categories, null, ['placeholder' => 'Pick a category'] ) !!}
    {!! Form::file('file') !!}
    {!! Form::token() !!}
    {!! Form::submit('Upload') !!}

    {!! Form::close() !!}

这是控制器

if($request->hasFile('file')){
        $upload->title = Auth::User()->id;
        $object = $request->users;
        $file = $request->file('file');
        $file->move(public_path(). '/', $file->getClientOriginalName());
        $upload->name = $file->getClientOriginalName(),
        $upload->size = $file->getClientsize();
        $upload->description = $request->description;
        $upload->cat = $request->cat;
        $upload->commentary = $request->commentary;
        $upload->type = $file->getClientMimeType();
        $upload->save();
    }

是表格名称是'文件'和列名称是' name'

0 个答案:

没有答案