Hello guys..
i tring to upload a profile picture and save the path into database..
but when i using custom request, the file path return as .tmp.
there is no problems when i using default Request class.
the $file_path variable return correct before saving it into database.
here is my code:
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\UpdateUserRequest $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UpdateUserRequest $request, $id)
{
// the request will validated by 'UpdateUserRequest' class
try {
// redirect if user dose not exist
$user = User::find($id);
if (!$user) {
return response()->json([
'errors' => ['The User Dose Not Exist!']
]);
}
// make accoount unverified when change it's status to not_verified
if (($user->status !== $request->get('status')) &&
$request->get('status') == 'not_verified'
) {
$user->forceFill([
'email_verified_at' => null
])->save();
}
// upload profile picture
// store file path to database
if ($request->hasFile('profile_picture')) {
// $request->file('profile_picture')->move(public_path('uploads/users/'), $request->file('profile_picture')->getClientOriginalName());
// $user->profile_picture = 'uploads/users/' . $request->file('profile_picture')->getClientOriginalName();
// $user->save();
$file = $request->file('profile_picture');
$file_name = $file->hashName();
$file_path = $file->storeAs('users', $file_name, 'local');
$user->profile_picture = $file_path;
$user->save();
}
// don't update password if user didn't change it
if (is_null($request->get('current_password')) && is_null($request->get('password'))) {
$user->update(
$request->safe()->except(['password', 'current_password', 'password_confirmation'])
);
} else {
// except this fields from update
// this fields doesn't exist in database
$user->update(
$request->safe()->except(['current_password', 'password_confirmation'])
);
}
return response()->json(['success' => 'Data successfully updated!']);
} catch (\Exception $ex) {
return response()->json([
'errors' => ['There Is Error!']
]);
}
in the 'UpdateUserRequest' file i have rules only:
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => [
'required', 'string', 'email', 'max:255',
Rule::unique('users', 'id')->ignore($this->user()->id),
],
'current_password' => ['nullable', 'current_password:web', 'required_with:password'],
'password' => [
'nullable', 'string', 'confirmed', 'required_with:current_password',
'different:current_password', Password::min(8)
],
'mobile' => ['nullable', 'numeric', 'digits_between:9,15'],
'birth_date' => ['nullable', 'date', 'date_format:Y-m-d', 'before_or_equal:' . date("Y-m-d", strtotime('-18 years'))],
'gender' => ['required', Rule::in(['male', 'female'])],
'role_id' => ['required', 'numeric', Rule::exists(Role::class, 'id')],
'status' => ['sometimes', 'string', Rule::in(['not_verified', 'verified', 'blocked'])],
'language' => ['required', 'string'],
'profile_picture' => [
File::image()
->types(['jpeg', 'jpg', 'png'])
->max(1024)
->dimensions(Rule::dimensions()->maxWidth(500)->maxHeight(500)),
],
];
}
any ideas to resolve this issue