/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($user, $id) { $album = Album::where(['id' => $id, 'user_id' => $user]); $is_del = true; if (count($album->get()->toArray())) { $images = Album::find($id); if (count($images) > 0) { $images = $images->images()->get()->toArray(); foreach ($images as $image) { $comment_img = Comment_image::where('image_id', $image['id']); if (count($comment_img->get()->toArray())) { if (false == $comment_img->delete()) { $is_del = false; } } if (false == unlink(public_path() . '/' . $image['fullsize_url']) || false == Image::destroy($image['id'])) { $is_del = false; break; } } } $album_id = $album->get()->first()->id; $comment = Comment_album::where('album_id', $album_id); if (count($comment->get()->toArray())) { if (false == $comment->delete()) { $is_del = false; } } if ($album->delete() && true == $is_del) { return redirect()->route('album.index', $user)->with(['message' => 'Album has been delete.']); } } return redirect()->route('album.index', $user)->withErrors('Can not delete album.'); }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($user, $id) { $image = Image::where(['id' => $id, 'user_id' => $user]); if (count($image->get()->toArray()) > 0) { if (count(Comment_image::find($id)) > 0) { Comment_image::where('image_id', $id)->delete(); } if (unlink(public_path() . '/' . $image->get()->first()->fullsize_url) && $image->delete()) { return redirect()->route('photo.index', $user)->with(['message' => 'The image has been delete.']); } } return redirect()->route('photo.index', $user)->withErrors('The image does not exists.'); }