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

[Bug] The upload field contains the wrong url #5232

Closed
CamusMX7 opened this issue Jul 26, 2023 · 7 comments
Closed

[Bug] The upload field contains the wrong url #5232

CamusMX7 opened this issue Jul 26, 2023 · 7 comments
Assignees
Labels
Possible Bug A bug that was reported but not confirmed yet. Priority: MUST

Comments

@CamusMX7
Copy link

CamusMX7 commented Jul 26, 2023

Bug report

What I did

I added the "withFiles" property that includes the path in a subfield of the repeatable field.

[  
                    'name'      => 'audioc',
                    'label'     => 'Audio mp3 menor a 4MB',
                    'type'      => 'upload',
                    'withFiles' => [
                        'disk' => 'public', // the disk where file will be stored
                        'path' => 'castings', // the path inside the disk where file will be stored
                    ],
                    'wrapper' => [
                        'class' => 'form-group col-md-2',
                    ]
                ]

What I expected to happen

It will upload the file and show the correct path to the file.

What happened

In the "href" it repeats the name of the path declared in "withFiles" and therefore the path is incorrect when clicking the link.

What I've already tried to fix it

Nothing yet

Is it a bug in the latest version of Backpack?

Yes

Backpack, Laravel, PHP, DB version

backpack/basset: 1.0.0
backpack/crud: 6.0.6
backpack/generators: v4.0.1
backpack/permissionmanager: 7.0.0
backpack/pro: 2.0.8
backpack/theme-coreuiv2: 1.1.3
backpack/theme-tabler: 1.0.5

repite path

@pxpm
Copy link
Contributor

pxpm commented Jul 26, 2023

Thanks for the report @CamusMX7

I fixed a similar issue for "non-repeatable" uploaders last week I guess I missed this usecase.

I will have a look at it ASAP 👍

Cheers

@pxpm pxpm self-assigned this Jul 26, 2023
@pxpm pxpm added Possible Bug A bug that was reported but not confirmed yet. Priority: MUST and removed triage labels Jul 26, 2023
@pxpm pxpm moved this to In Progress in This week Jul 26, 2023
@pxpm
Copy link
Contributor

pxpm commented Jul 26, 2023

Fixed the issue in: #5233

Just tagged https://github.com/Laravel-Backpack/CRUD/releases/tag/6.0.7 along with some other fixes.

Let me know if it still does not work for you @CamusMX7 🙏

Thanks

@pxpm pxpm closed this as completed Jul 26, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Done in This week Jul 26, 2023
@CamusMX7
Copy link
Author

CamusMX7 commented Jul 26, 2023

@pxpm I keep seeing the same problem with the URL I already had saved (castings/1-1-tSzR.mp3). "The path (castings)" continues to be duplicated.
I tried to generate a new file, but after the upgrade to 6.0.7 two more problems came up.

  1. I was retrieving the data in my function setLocutoresAttribute($value) because I need to insert the records that can be duplicated in a pivot table. In version 6.0.6 I was receiving a JSON with the corresponding values, now it seems that I am receiving an ARRAY and it does not contain the URL of the UPLOAD field, so I cannot insert the URL into the Database. How should I now retrieve the URL to register in the database? because the value of the field that should have the URL arrives empty. And why the change of how the values of the form are sent from JSON to ARRAY?

  2. When I was going to do tests, it sent me a validation message, that a field was missing. The field of a relation, which does have a value but for some reason isn't displaying it:

Controller

CRUD::field('cliente')->allows_null(true)->wrapper([
            'class' => 'form-group col-md-12 pl-0 pr-0',
        ]);

model

public function cliente(): BelongsTo
    {
        return $this->belongsTo(Cliente::class);
    }

Even in the list it is displayed correctly, but in the form it is not.

listado
form

UPDATED

I forgot to mention that I did a general composer update.
About point 2:
That field stopped working in all my forms.
If I add ajax(true) now if it shows the value, but it should work as before

CRUD::field('cliente')->allows_null(true)->ajax(true)->wrapper([
            'class' => 'form-group col-md-12 pl-0 pr-0',
]);

@pxpm
Copy link
Contributor

pxpm commented Jul 27, 2023

Hey @CamusMX7 thanks for the report.

But let's not mix issues or it gets very difficult to me to keep up with what you report.

If needed always open a new issue with a different topic, we don't charge by issues opened 😄

Let's focus on the upload issue here.

I've just reverted my last PR. And I wasn't able to get json on the setter.

// the model
protected $fillable = ['name', 'extras', 'meta'];

protected $casts = ['meta' => 'json'];

public function setMetaAttribute($value) {
    dd($value, $this);
}

// the controller
CRUD::field('name')->type('upload')->withFiles(['path' => 'olanina']);
CRUD::field('meta')->type('repeatable')->subfields([
    [
        'name' => 'test',
        'type' => 'text'
    ],
    [
        'name' => 'test_upload',
        'type' => 'upload_multiple',
        'withFiles' => [
            'path' => 'testpath'
        ],
    ],
]);

And when I submit the form and dd() in setMetasAttribute I always get an array:
image

What I am missing @CamusMX7 ?

@CamusMX7
Copy link
Author

Sorry for mixing issues, but since the update I've been suffering a bit.

Concentrating on the upload field in repeatable. If I use it by saving the json in a field of my database, EVERYTHING works fine, the data is saved, the URL and it shows the url correctly in the form.

But if I try to store in a pivot table now nothing related to the upload field works fine.

DB

- castings: id, others
- locutores: id, others
- casting_locutor: id, casting_id, locutor_id, audioc

If I insert the url of the file directly into the database, for example in the "audioc" field: "castings/file.mp3" when it is displayed in the form it keeps repeating "castings/castings/file.mp3" which was my initial problem.

Now, with your example trying to replicate the situation, can you delete the url of your file and try getting the new file?
Because the fields are received in the setter, but they are the values that are stored in the database, they are not the new values that are sent from the form. That is, if I try to add a new file, I receive the value that is in the database, not the value that the form sends me. So now I can't save the new file name.

To be clearer, could you print what you receive with the following? and could you confirm that you receive the new value of the upload field and not the value stored in the database?

// the model
protected $fillable = ['meta'];

protected $casts = ['meta' => 'json'];

public function setMetaAttribute($value) {
    dd($value, $this);
}

// the controller
CRUD::field('meta')->type('repeatable')->subfields([
    [
        'name' => 'test',
        'type' => 'text'
    ],
    [
        'name' => 'test_upload',
        'type' => 'upload',
        'withFiles' => [
            'path' => 'testpath'
        ],
    ],
]);

@pxpm
Copy link
Contributor

pxpm commented Jul 29, 2023

Hey @CamusMX7 thanks for getting back.

I could reproduce your issue and I am working on the fix. #5243

The previous fix I made like you said, only accounted for repeatables not relations. You are working with a BelongsToMany relationship, and that needs some special handling.

I am almost done, If I have a little time during this weekend I may be able to finish it, otherwise I finish it at the beginning of next week.

Thanks again 🙏

@CamusMX7
Copy link
Author

CamusMX7 commented Oct 24, 2023

Hello @pxpm , Is there any news about this? I see that it is marked as closed, but I never found a solution.
I even changed my implementation for other themes. I neutralized the setMetaAttribute and now save the relationship fields from (in the controller setupCreateOperation)

Casting::saved(function($entry) {
             $request = $this->crud->getRequest()->request->all();
             dd($request);
}

But I still can't get the URL to store it in the database.
In other words, the file is still being sent correctly to the specified path, but I can't link to it because I can't get that URL to store in the database.

Thanks.


UPDATE

Sorry, I had not reviewed in detail what was proposed in:
https://backpackforlaravel.com/docs/6.x/crud-uploaders#handling-uploads-in-relationship-fields

Which apparently was the solution to this issue.
This apparently solves most of the cases I have pending. I already tried it and it works well.

BUT, what happens in specific cases where I handle relationships differently?
For example, a year ago, I think you suggested for my problem, not to use the "relationship" field type in my "belongsToMany" relationship, because I needed to be able to repeat elements, so I used the "repeatable" field type. and change my table with an incremental ID. Create a "setSomethingAttribute" to get that json and insert the values into my pivot table. Just as I initially presented this case. How do I get the "url" of the file I'm loading?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Possible Bug A bug that was reported but not confirmed yet. Priority: MUST
Projects
Status: Done
Development

No branches or pull requests

2 participants