PUBLISHED 6 MONTHS AGO BY WIZIX
Hey guys, I have this method allowing me to update the information of an user profile :
public function update_info(UpdateProfileInfo $request, User $user)
{
if ($request->hasFile('picture'))
{
Storage::delete($user->picture); // Delete the picture if it already exists
$ext = '.' . $request->file('picture')->extension();
$user->picture = $request->file('picture')->storeAs('pictures', $user->id . $ext, 'public');
$request->files->remove('picture'); // Delete the key, because we already manually changed the value
}
$user->update($request->all());
return redirect()->route('profile.show', ['user' => $user]);
}
If there is a picture uploaded, I manually update the model, delete the key and then update automaticly the other fields. But it doesn't work as expected because, before $user->update()
, I have the right path for my picture and after the update, I have /tmp/phpmFOzBt
. Why ?
Thanks !
Best Answer(As Selected By Wizix)
Files get uploaded to the tmp directory by default. You should always move those files to a different location.
Visit https://laravel.com/docs/5.5/filesystem#file-uploads for more information.
Afterwards you could always update or overwrite the request data with the new file path. Another option is to use $request->except('picture') and add that value manually to the update method with the correct path.
Look into using $request->only([]) instead of ->all() so that you only update the fields you want and don't overwrite the file path.
Please sign in or create an account to participate in this conversation.
来自 https://laracasts.com/discuss/channels/laravel/delete-parameters-from-request
It's posible to validate request with rules for additional fields, or remove that fields from request?
Simple example, I have FormRequest object with rules:
public function rules() {
return [
'id' => 'required|integer',
'company_name' => 'required|max:255',
];
}
And when I get post request with any other fields I want to get error/exception in controller or I want to get only id and company_name fields, with no any others. It's exist any feature in laravel with that, or I must make it in my way?