简体   繁体   中英

Laravel Voyager BREAD Image Edit and Delete Issue

I have ran into a problem with image upload using voyager BREAD System. If I delete or update an image using BREAD the old image not replaced or deleted. It is still in the storage directory. I was using latest version of voyager with laravel 5.5. Is there any solution to this problem? Thank you in advance.

Hey please check below file in your project's vendor/tcg/voyager/src/http/controllers/VoyagerBreadController.php directory

and check for this

public function deleteBreadImages($data, $rows) {...}

function at line number 403.

in this function find $this->deleteFileIfExists($data->{$row->field}); make it un-comment and check.

may i hope this helps you

There is no public function deleteBreadImages($data, $rows) {...} anymore
in Laravel Voyager 1.2 class file
vendor/tcg/voyager/src/http/controllers/VoyagerBreadController.php

After two days of googling same issue I figured out the Hard Solution...
in my case image field name is img and model name is Company
code is executed while the Model BREAD is updating

Works on Voyager 1.2 Hope it helps ))

use Storage;

class Company extends Model
{
    public static function boot()
    {
        parent::boot();

        static::updating(function($model)
        {   
            // Check if Old File Exists
            $oldFileExists = Storage::disk('public')->exists($model->original['img']);

            // If Old File Exists DELETE it, else Continue Adding New Image
            if($oldFileExists)
            {
                //Get File Extension:: .jpg .png .gif
                $fileExt = substr(strrchr($model->original['img'], "."), 0);

                //If File is not .GIF
                if($fileExt != '.gif'){
                    // Delete Old Non-GIF Image
                    Storage::disk('public')->delete($model->original['img']);
                }

                // Find .gif , -static.gif Old Images And Delete
                else{

                    // filename-static.gif
                    $staticOld = str_replace($fileExt,"-static".$fileExt,$model->original['img']);
                    // Delete Old Image.gif
                    Storage::disk('public')->delete($model->original['img']);
                    // Delete Old Image-static .gif if Exists
                    if($staticOld) Storage::disk('public')->delete($staticOld);

                }
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM