Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.18k stars 897 forks source link

[Bug] crud-api doesn't work properly with upload fields. #5644

Closed rezahmady closed 1 day ago

rezahmady commented 2 months ago

Bug report

What I did

in store method i want to change field : my field :

[
    'name'  => 'logoImage',
    'label' => trans("serial::lang.video_logo_image"),
    'type' => 'image',
    'withFiles' => [
        'disk' => $static_disk,
        'path' => 'images',
    ],
    'wrapper'   => [
        'class'  => "form-group col-md-6 ltr"
    ],
    'fake'  => true,
    'tab'   => trans('vod::lang.video_images_tab'),
],

i want to change field value :

$crud->getRequest()->request->add([
    'imagePoster' => $value,
]);

or

$this->crud->modifyField('imagePoster', [ 'value' => $value]);

This api works well with fields that do not have the withFiles attribute

What I expected to happen

field value save correctly.

What happened

field value is null.

I know that you are working on the redesign of the upload field, but this issue is very important to me and I cannot wait until you finish that task. If possible, check and fix this issue.

karandatwani92 commented 1 month ago

Hey @rezahmady

I just tested it, and it works for me. Here is what I did:

  1. Followed the guide
  2. Used the following code to modify the image on the fly.
    
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate;}

public function update(){ $single_pixel_base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=="; // modify image before saving $this->crud->getRequest()->request->add(['main_image' => $single_pixel_base64]); $response = $this->traitUpdate(); // do something after update return $response; }

public function store(){ $single_pixel_base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=="; // modify image before saving $this->crud->getRequest()->request->add(['main_image' => $single_pixel_base64]); $response = $this->traitStore(); // do something after save return $response; }


Field Definition:
```php
CRUD::field('main_image')
    ->label('Main Image')
    ->type('image')
    ->tab('Media')
    ->wrapper(['class' => 'form-group col-md-4'])
    ->fake(true)
    ->withFiles(['path' => 'images']);