Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V6 Uploaders - deleteWhenEntryIsDeleted not working #5234

Closed
pxpm opened this issue Jul 26, 2023 · 3 comments
Closed

V6 Uploaders - deleteWhenEntryIsDeleted not working #5234

pxpm opened this issue Jul 26, 2023 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation Priority: MUST

Comments

@pxpm
Copy link
Contributor

pxpm commented Jul 26, 2023

Discussed in Laravel-Backpack/community-forum#586

Originally posted by christmex July 26, 2023
Hi guys, I have a problem with V6 uploaders, can you guys help me out?

What do I want?

When I delete the model/entry it will also delete the picture file

What did I do?

reading the documentation and trying it, also I already solved it but with v5 ways

What is actually happening?

It did not delete the picture file when I delete the model/entry

Here's my backpack version

### PHP VERSION:
PHP 8.1.10 (cli) (built: Aug 30 2022 18:05:49) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies

### LARAVEL VERSION:
10.16.1.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.0.2
backpack/crud: 6.0.6
backpack/generators: v4.0.2
backpack/theme-tabler: 1.0.6

Here's my controller

        CRUD::field('member_profile_picture')
            ->type('upload')
            ->withFiles([
                'disk' => 'public', // the disk where file will be stored
                'path' => '/uploads/member/', // the path inside the disk where file will be stored
        ]);

I already solve it with backpack v5 ways, something like

//inside my related model
public static function boot()
    {
        parent::boot();
        static::deleting(function($obj) {
            if($obj->member_profile_picture){
                \Storage::disk('public')->delete($obj->member_profile_picture);
            }
        });
    }

Did I miss something? let me know, thank you 🙏.

@pxpm pxpm transferred this issue from Laravel-Backpack/community-forum Jul 26, 2023
@pxpm pxpm self-assigned this Jul 26, 2023
@pxpm pxpm added documentation Improvements or additions to documentation Priority: MUST labels Jul 26, 2023
@pxpm
Copy link
Contributor Author

pxpm commented Jul 26, 2023

This is missing documentation.

To properly have the deleting feature working, the developer should setup the fields (where the uploaders are registered), in their DeleteOperation.

public function setupDeleteOperation()
{
    CRUD::field('photo')->type('upload')->withFiles();
    // OR $this->setupCreateOperation();
}

Usually you can store the fields in a getter function or a property in the controller and reuse it:

private function setupFields() 
{
CRUD::field('photo')->type('upload')->withFiles();
}

public function setupDeleteOperation()
{
   $this->setupFields();
}

public function setupCreateOperation()
{
   $this->setupFields();
}

Will add proper docs for it.

@christmex
Copy link

Thank you @pxpm

@pxpm
Copy link
Contributor Author

pxpm commented Jul 26, 2023

Fixed in Laravel-Backpack/docs#479

@pxpm pxpm closed this as completed Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation Priority: MUST
Projects
Status: Done
Development

No branches or pull requests

2 participants