mercredi 29 janvier 2020

Not delete image from laravel backpack 4.0

I'm using image field in laravel backpack 4.0, it upload image without problems. When I delete it from 'delete button', it delete register, but not image file from my local folder. I'm checked the answer from backpack for laravel deleting image , but It no fix my issue.

My config/filesystem:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
    ],

],

My Model code:

public function setImageAttribute($value)
{
    $attribute_name = "image";
    $disk = config('backpack.base.root_disk_name'); // or use your own disk, defined in config/filesystems.php
    $destination_path = env('FOLDER_PUBLIC')."/uploads/medias"; // path relative to the disk above

    // if the image was erased
    if ($value==null) {
        // delete the image from disk
        \Storage::disk($disk)->delete($this->{$attribute_name});

        // set null in the database column
        $this->attributes[$attribute_name] = null;
    }

    // if a base64 was sent, store it in the db
    if (starts_with($value, 'data:image'))
    {
        // 0. Make the image
        $image = \Image::make($value)->encode('jpg', 90);
        // 1. Generate a filename.
        $filename = rand ( 10000 , 99999 ).'-'.strtolower(trim(preg_replace('/[\s-]+/', '-', preg_replace('/[^A-Za-z0-9-]+/', '-', preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $this->title))))), '-')).'.jpg';

        // 2. Store the image on disk.
        \Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
        // 3. Save the public path to the database
        // but first, remove "public/" from the path, since we're pointing to it from the root folder
        // that way, what gets saved in the database is the user-accesible URL
        $public_destination_path = Str::replaceFirst(env('FOLDER_PUBLIC').'/', '', $destination_path);
        $this->attributes[$attribute_name] = $public_destination_path.'/'.$filename;
    }
}
public static function boot()
{
    parent::boot();

    static::deleting(function($obj) {

        \Storage::disk('public')->delete($obj->image);
    });
}

I have tried to change:

\Storage::disk('public')->delete($obj->image);

By:

\Storage::disk(config('backpack.base.root_disk_name'))->delete($obj->image);

But it not working too,

Can you help to me ?

Sorry for my english



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire