Search code examples
phplaravel-8laravel-9php-8

I can not delete any video What is the cause of this problem?


League\Flysystem\Filesystem::delete(): Argument #1 ($location) must be of type string, null given, called in C:Desktop\video_platform\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php on line 496

public function destroy($id)
{
    $video = Video::where('id' , $id)->first();
    $convertedVideos = Convertedvideo::where('video_id' , $id)->get(); 
    foreach($convertedVideos as $convertedVideo){
        Storage::delete([
            $convertedVideo->mp4_Format_240,
            $convertedVideo->mp4_Format_360,
            $convertedVideo->mp4_Format_480,
            $convertedVideo->mp4_Format_720,
            $convertedVideo->mp4_Format_1080,
            $convertedVideo->webm_Format_240,
            $convertedVideo->webm_Format_360,
            $convertedVideo->webm_Format_480,
            $convertedVideo->webm_Format_720,
            $convertedVideo->webm_Format_1080,
            $video->image_path
        ]);
    }
    $video->delete();
    return back()->with('success' , 'Completed');
}

Solution

  • Some of the array elements are null and the method used to delete the file only accepts the string type (the file path), so the error is returned. Try checking its elements before passing the array. If you do not want to check all the fields and all these fields are required in your business logic, try to make these columns required in your model/table.

    Ex:

    public function destroy($id)
    {
        $video = Video::where('id' , $id)->first();
        $convertedVideos = Convertedvideo::where('video_id' , $id)->get(); 
        foreach($convertedVideos as $convertedVideo){
            
            // array_filter will remove all elements equals to FALSE
            $paths = array_filter([
                $convertedVideo->mp4_Format_240,
                $convertedVideo->mp4_Format_360,
                $convertedVideo->mp4_Format_480,
                $convertedVideo->mp4_Format_720,
                $convertedVideo->mp4_Format_1080,
                $convertedVideo->webm_Format_240,
                $convertedVideo->webm_Format_360,
                $convertedVideo->webm_Format_480,
                $convertedVideo->webm_Format_720,
                $convertedVideo->webm_Format_1080,
                $video->image_path
            ]);
    
            Storage::delete($paths);
        }
        $video->delete();
        return back()->with('success' , 'Completed');
    }