public function changeProfilePicture($user, UpdateProfilePictureRequest $request) { if ($request->hasFile('image')) { $file = $request->file('image'); $album = Album::where(['album_name' => 'Profile picture', 'user_id' => $this->user->id])->get()->first(); if (count($album)) { $albumId = $album->id; } else { if ($newAlbum = Album::create(['album_name' => 'Profile picture', 'album_title' => 'Profile picture', 'album_description' => '', 'user_id' => $this->user->id])) { $albumId = $newAlbum->id; } else { return response('Unexpected error!', 422); } } $destination = public_path() . '/upload/images/' . $this->user->id . '_' . $this->folder . '/avatar'; if (!file_exists(public_path() . '/upload/images/' . $this->user->id . '_' . $this->folder)) { mkdir(public_path() . '/upload/images/' . $this->user->id . '_' . $this->folder); } if (!file_exists($destination)) { mkdir($destination); } $imageName = date('d-m-Y_h-i-s') . '_' . $this->user->id . '_' . $file->getClientOriginalName(); $imageUrl = 'upload/images/' . $this->user->id . '_' . $this->folder . '/avatar/' . $imageName; $imageArr = ['image_name' => $imageName, 'fullsize_url' => $imageUrl, 'image_size' => $file->getSize(), 'image_caption' => '', 'user_id' => $this->user->id, 'album_id' => $albumId, 'make_as_profile_picture' => 1]; $changeImage = Image::find($this->hasProfilePicture($this->user->id)); if (!is_null($changeImage)) { $changeImage->make_as_profile_picture = 0; $changeImage->save(); } if ($file->move($destination, $imageName) && Image::create($imageArr)) { $data['errors'] = 0; $data['msg'] = 'Your profile picture has been change!'; $data['imageUrl'] = url($imageUrl); return json_encode($data); } return response('Unexpected error while changging your profile picture!', 422); } }