我已将文件上传(个人资料图片)添加到注册表单中,该表单由Laravel 5.3中的命令'make:auth'生成 问题是我得到一个字符串而不是文件所以我不能使用move()函数将文件放在应该的位置。
这是我的表格:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{{ csrf_field() }}
{!! Form::file('profile_picture', null, ['class' => 'form-control']) !!}
<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
<input id="first_name" type="text" class="form-control" name="first_name" value="{{ old('first_name') }}" placeholder="First name" required autofocus>
@if ($errors->has('first_name'))
<span class="help-block">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
<input id="last_name" type="text" class="form-control" name="last_name" value="{{ old('last_name') }}" placeholder="Last name" required autofocus>
@if ($errors->has('last_name'))
<span class="help-block">
<strong>{{ $errors->first('last_name') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
这是我的创建用户功能:
protected function create(array $data)
{
$user = User::create([
'email' => $data['email'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'password' => bcrypt($data['password']),
'class' => $data['class'],
'year' => $data['year'],
'phone' => $data['phone']
]);
$tags = $data['tags'];
$user->tags()->sync($tags); //Pivot table of tags
if($data['profile_picture']){
$file = File::create(['name' => $data['profile_picture'] , 'user_id' => $user->id]);
MOVE THE FILE WITH $file->move();
$file->save();
$user->files()->attach($file); //Pivot table of files
}
return $user;
}
答案 0 :(得分:0)
尝试在表单HTML标记中使用enctype="multipart/form-data"
。