Laravel-Backpack / CRUD

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

Backpack laravel multiple file upload - preview/download is not working #5546

Closed blessen131 closed 3 months ago

blessen131 commented 3 months ago

I am trying to upload/view multiple files with Backpack:5.x

Code for the file upload

Controller

 $this->crud->addfield([
            'name'    => 'files',
            'label'   => 'Files',
            'upload'    => true,
            'type'    => 'upload_multiple',
        ]);

Model

public function setFilesAttribute($value)
    {
        $attribute_name = "files";
        $disk = "uploads";
        $destination_path = "notes";

        $this->uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path);
    }

config/filessystems.php

'uploads' => [
            'driver' => 'local',
            'url' => env('APP_URL').'/uploads',
            'root' => public_path('uploads'),
        ],

Problem 1: Files saved in public/uploads/notes directory but when I edit an item, the url for the uploaded file is www.test.com/notes/file.txt getting 404(missing the /uploads/notes/file.txt).

Problem 2: When I am trying to add in list view, I am getting this error - count(): Parameter must be an array or an object that implements Countable (View: /vendor/backpack/crud/src/resources/views/crud/columns/upload_multiple.blade.php)


$this->crud->addColumn([
            'name'    => 'files',
            'label'   => 'Files',
            'type'    => 'upload_multiple',
        ]);
welcome[bot] commented 3 months ago

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use GitHub Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication channels:

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

-- Justin Case The Backpack Robot

karandatwani92 commented 3 months ago

Hey @blessen131

I'm assuming you have an upload disk defined in config/filesystems.php.

From your code, I can see that:

  1. You are saving the file to $disk = "uploads"; but you have not defined the same disk in the column or field for reading. Please add 'disk' => 'upload' to the column & field definition.
  2. You need to cast the field to the array in your model:
    protected $casts = [
    'files' => 'array',
    ];

I hope this helps. I recommend you upgrade to v6. It comes with automatic file handling/uploading/deletion features and many other valuable features. Support for v5 has been discontinued, and Community-forum would be the right place for queries like this.

See you around. Have a great day.