我在设置头像名称时遇到问题,因此我可以将其放到数据库中,它显示一个对象而不是文件名,如您所见,我已经echo $filename
以确保我拥有该名字图片。但是当我print_r($sanitized)
时,图像是一个对象。
我对$sanitized
的预期结果应该是:
Array
(
[email] => superadmin@email.com
[name] => Superadmin
[phone] => 123123
[avatar] => 1_avatar1546579727.jpg
)
代码:
public function updateProfile(Request $request)
{
$this->setUser($request);
$user = $this->user;
// Validate the request
$this->validate($request, [
'email' => ['sometimes', 'email', Rule::unique('users', 'email')->ignore($this->user->getKey(), $this->user->getKeyName()), 'string'],
'name' => ['nullable', 'string'],
'phone' => ['sometimes', 'string'],
'avatar' => ['sometimes', 'image', 'mimes:jpeg,png,jpg,gif', 'dimensions:min_width=500,min_height=500', 'max:2048'],
], [
'avatar.mimes' => 'Uploaded file format should be jpeg, jpg, png or gif.',
'avatar.dimensions' => 'Image should have minimum 200x200px dimensions.',
'avatar.max' => 'Maximum allowed file size is 2 MB.',
]);
if($request->hasFile('avatar')) {
$filename = $user->id.'_avatar'.time().'.'.request()->avatar->getClientOriginalExtension();
Image::make(request()->avatar)->resize(300, 300)->save( public_path('uploads/avatars/'.$filename) );
// $request->avatar = $filename;
$request['avatar'] = $filename;
}
// Sanitize input
$sanitized = $request->only([
'email',
'name',
'phone',
'avatar'
]);
echo $filename . "</br>";
echo "<pre>";
print_r( $sanitized );
echo "</pre>";
return "";
// $this->user->update($sanitized);
// return redirect()->back()->with('success', 'Profile has been updated.');
}
编辑1
我已经尝试了所有答案,但结果仍然相同。 代码:
if($request->hasFile('avatar')) {
$filename = $user->id.'_avatar'.time().'.'.request()->avatar->getClientOriginalExtension();
Image::make(request()->avatar)->resize(300, 300)->save( public_path('uploads/avatars/'.$filename) );
//This is what I've tried so far below:
$request->request->add(['avatar', $filename]);
// $request->merge(['avatar' => $filename]);
// $request->avatar = $filename;
// $request['avatar'] = $filename;
}
答案 0 :(得分:1)
我已经通过提示给我@ Md.Sukel Ali
来修复它我将$sanitzed = $request..
移到了顶部,然后对其进行了更新,而没有直接使用$request
变量。
工作代码:
public function updateProfile(Request $request)
{
$this->setUser($request);
$user = $this->user;
// Validate the request
$this->validate($request, [
'email' => ['sometimes', 'email', Rule::unique('users', 'email')->ignore($this->user->getKey(), $this->user->getKeyName()), 'string'],
'name' => ['nullable', 'string'],
'phone' => ['sometimes', 'string'],
'avatar' => ['sometimes', 'image', 'mimes:jpeg,png,jpg,gif', 'dimensions:min_width=500,min_height=500', 'max:2048'],
], [
'avatar.mimes' => 'Uploaded file format should be jpeg, jpg, png or gif.',
'avatar.dimensions' => 'Image should have minimum 200x200px dimensions.',
'avatar.max' => 'Maximum allowed file size is 2 MB.',
]);
// Sanitize input
$sanitized = $request->only([
'email',
'name',
'phone',
'avatar'
]);
if($request->hasFile('avatar')) {
$filename = $user->id.'_avatar'.time().'.'.request()->avatar->getClientOriginalExtension();
Image::make(request()->avatar)->resize(300, 300)->save( public_path('uploads/avatars/'.$filename) );
$sanitized['avatar'] = $filename;
}
echo $filename . "</br>";
echo "<pre>";
print_r( $sanitized );
echo "</pre>";
return "";
// $this->user->update($sanitized);
// return redirect()->back()->with('success', 'Profile has been updated.');
}