我在这里需要else-if语句吗?还是不必要?

时间:2018-10-14 16:54:09

标签: php laravel

此功能用于删除图像。如果尝试删除映像的用户不是映像的作者或管理员,则第一个if重定向回,否则,if-if检查用户是否具有管理员角色,如果拥有,则删除该映像。最后,如果用户不是管理员,而是图像的作者,那么最后一部分代码也会删除该图像。

现在我的问题是,如果代码的一部分,我是否还需要else?我能否只用一个小的if语句来重定向用户(既不是管理员也不是作者),然后删除图像,因为如果用户通过第一个if,则肯定是管理员或作者。

public function deleteImage($imageId){
        $image = Image::where('id', $imageId)->first();
        if (!Auth::user()->hasRole('Admin')) {
            if (Auth::user() != $image->user){
                return redirect()->back();
            }
        } else if (Auth::user()->hasRole('Admin')) {
            $imageName = $image->image_file_name;
            $image->tags()->detach();
            $image->delete();
            Storage::delete('public/uploads/images/'.$imageName);
            Storage::delete('public/uploads/images/thumbnails/'.$imageName);
            Storage::delete('public/uploads/images/specificImages/'.$imageName);
            Storage::delete('public/uploads/images/miniImages/'.$imageName);
            $imageChildren = Image::where('parent_id', $image->id)->get();
            foreach ($imageChildren as $imageChild) {
                Storage::delete('public/uploads/images/'.$imageChild->image_file_name);
                Storage::delete('public/uploads/images/thumbnails/'.$imageChild->image_file_name);
                Storage::delete('public/uploads/images/specificImages/'.$imageChild->image_file_name);
                Storage::delete('public/uploads/images/miniImages/'.$imageChild->image_file_name);
                $imageChild->delete();
            }
            return redirect()->route('home');
        }
        $imageName = $image->image_file_name;
        $image->tags()->detach();
        $image->delete();
        Storage::delete('public/uploads/images/'.$imageName);
        Storage::delete('public/uploads/images/thumbnails/'.$imageName);
        Storage::delete('public/uploads/images/specificImages/'.$imageName);
        Storage::delete('public/uploads/images/miniImages/'.$imageName);
        $imageChildren = Image::where('parent_id', $image->id)->get();
        foreach ($imageChildren as $imageChild) {
            Storage::delete('public/uploads/images/'.$imageChild->image_file_name);
            Storage::delete('public/uploads/images/thumbnails/'.$imageChild->image_file_name);
            Storage::delete('public/uploads/images/specificImages/'.$imageChild->image_file_name);
            Storage::delete('public/uploads/images/miniImages/'.$imageChild->image_file_name);
            $imageChild->delete();
        }
        return redirect()->route('home');
    }

1 个答案:

答案 0 :(得分:1)

下面应该做同样的事情。如果不需要,则不需要else,因为一旦满足第一个条件,它将始终执行相同的执行。我还连接了第一个条件,其中包括对作者和图像用户的检查

public function deleteImage($imageId){
    $image = Image::where('id', $imageId)->first();
    if (!Auth::user()->hasRole('Admin') && Auth::user() != $image->user) {
            return redirect()->back();
    }
    $imageName = $image->image_file_name;
    $image->tags()->detach();
    $image->delete();
    Storage::delete('public/uploads/images/'.$imageName);
    Storage::delete('public/uploads/images/thumbnails/'.$imageName);
    Storage::delete('public/uploads/images/specificImages/'.$imageName);
    Storage::delete('public/uploads/images/miniImages/'.$imageName);
    $imageChildren = Image::where('parent_id', $image->id)->get();
    foreach ($imageChildren as $imageChild) {
        Storage::delete('public/uploads/images/'.$imageChild->image_file_name);
        Storage::delete('public/uploads/images/thumbnails/'.$imageChild->image_file_name);
        Storage::delete('public/uploads/images/specificImages/'.$imageChild->image_file_name);
        Storage::delete('public/uploads/images/miniImages/'.$imageChild->image_file_name);
        $imageChild->delete();
    }
    return redirect()->route('home');
}