@hyper_bebrik

Почему через postman картинка загружается, а через axios — нет?

vue
const formData = new FormData();
      formData.append('name', user.name);
      formData.append('surname', user.surname);
      formData.append('group', user.group);
      formData.append('email', user.email);
      formData.append('role', user.role);
      formData.append('image', user.image);
      const config = { headers: { "content-type": "multipart/form-data" }}
      delete user.image_url;

      let response;
      if (user.id) {
        response = axiosClient
          .post(`/users/${user.id}`, formData, config)
          .then((res) => {
            commit('setCurrentUser', res.data)
            // if (user.id === rootState.user.data.id){
            //   dispatch('getUser', null, { root: true});
            // }
            return res;
          });
      }


Laravel
public function update(Request $request,User $user)
    {
        $data = $request->all();

        $validator = Validator::make($data, [
            'name' => 'required|string',
            'surname' => 'required|string',
            'email' => [
                "required",
                Rule::unique('users', 'email')->ignore($user->id),
            ],
            'role' =>'required|string',
            'patronymic' => 'string|nullable',
            'group_id' => 'string|exists:groups,id|nullable',
            'tutor_of' => 'string|exists:groups,id|nullable',
            'status' => 'string',
            'password' => [
                Password::min(8)->mixedCase()->numbers()->symbols()
            ],
            'image' => 'mimes:jpg,jpeg,png|max:5048',
        ]);

        if ($validator->fails()) {
            return response(['error' => $validator->errors(), 'Validation Error']);
        }

        $user->name = $request->name;
        $user->surname = $request->surname;
        $user->email = $request->email;
        if($request->has('patronymic')) {
            $user->patronymic = $request->patronymic;
        }
        if($request->has('password')) {
            $user->password = bcrypt($request->password);
        }
        if($request->has('status')) {
            $user->status = bcrypt($request->status);
        }
        if ($request->has('group_id')){
            $user->group_id = $request->group_id;
            $user->tutor_of = null;
        }
        if ($request->has('tutor_of')){
            $user->tutor_of = $request->tutor_of;
            $user->group_id = null;
        }
        if ($request->file()){

            if ($user->avatar_location !== null){
                Storage::disk('public')->delete(str_replace('http://localhost:8000/storage/', '', $user->avatar_location));
            }
            $newImageName = time() . '-' . $request->name . '.' . $request->image->extension();
            $filePath = Storage::disk('public')->putFileAs('/images',$request->image, $newImageName);
            $url = url('/storage/' . $filePath);
            $user->avatar_location = $url;
        }


        $user->removeRole($user->roles[0]->name);
        $user->assignRole($request->role);
        $user->save();

        return response(['user' => new UserResource($user), 'message' => 'Updates successfully'], 200);
    }

634cf5257aa6f916527832.png
634cf5319b39e197130860.png
  • Вопрос задан
  • 135 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы