CRUD: laravel backpack upload field is not moving image to my path, but its storing path to database

controller

 $this->crud->addField([   // Upload
    'name' => 'image',
    'label' => 'Image',
    'type' => 'upload',
    'upload' => true,
    'disk' => 'uploads' // if you store files in the /public folder, please ommit this; if you store them in /storage or S3, please specify it;
]);

model

public function setImageAttribute($value)
    {
        $attribute_name = "image";
        $disk = "public";
        $destination_path = "/uploads";       
        $this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
    }

config/filesystem.php

'uploads' => [
                    'driver' => 'local',
                    'root' => public_path().'/uploads',
                    // 'url' => '/photos/',
                    // 'visibility' => 'public',
                ],

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 30 (12 by maintainers)

Most upvoted comments

here is my setting which can successfully access uploaded image:

` ‘uploads’ => [ // used for Backpack/CRUD (in elFinder) ‘driver’ => ‘local’, ‘root’ => public_path(‘/’), ],

$this->crud->addField([ ‘label’ => ‘image’, ‘type’ => ‘upload’, ‘name’ => ‘filename’, ‘upload’ => true, ]);

public function setFilenameAttribute($value) { $attribute_name = “filename”; $disk = “uploads”; $destination_path = “frames_part”;

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

}

`

Hope it helps

FIRST MAKE SURE THAT THE DISK IS LIKE THIS

'uploads' => [
            'driver' => 'local',
            'root' => public_path('uploads'),
        ],

and your model like that

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
use CrudTrait;
use SoftDeletes;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/

protected $table = 'products';
protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $fillable = ['name','description','image','category_id','price'];
// protected $hidden = [];
protected $dates = ['deleted_at'];


public function category() {
    return $this->hasOne('App\Models\Category', 'id', 'category_id')->withTrashed();;
}

public function cat() {
    return $this->category->name;
}

public function setImageAttribute($value)
{

    $attribute_name = 'image';
    $disk = 'uploads';
    $destination_path = 'images';
  
if (starts_with($value, 'data:image')) {
                    // 0. Get image extension
                    preg_match("/^data:image\/(.*);base64/i", $value, $match);
                    $extension = $match[1];
                    // 1. Make the image
                    $image = Image::make($value);
                    if (!is_null($image)) {
                        // 2. Generate a filename.
                        $filename = md5($value.time()).'.'.$extension;

                        try {
                            // 3. Store the image on disk.
                            Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
                            // 4. Save the path to the database
                            $this->attributes[$attribute_name] = $destination_path.'/'.$filename;
                        } catch (\InvalidArgumentException $argumentException) {
                            // 3. failed to save file
                            Alert::error($argumentException->getMessage())->flash();
                            // 4. set as null when fail to save the image to disk
                            $this->attributes[$attribute_name] = null;
                        }
                    }
                } else {
                    $this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
                }
}

/* public static function boot()
{
parent::boot();
static::deleting(function($obj) {
\Storage::disk('uploads')->delete($obj->image);
});
}*/

/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| ACCESORS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}

can anyone plzz let me know that what changes should i have to do to make my disk as s3

only you can reopen bcz you closed it so

I’m not convinced this is a bug yet.

Do:

public function setImageAttribute($value)
    {
        $attribute_name = "image";
      if (request()->hasFile($attribute_name) && request()->file($attribute_name)->isValid()) {
   throw new \Exception('IT SHOULD WORK'); }

        $disk = "public";
        $destination_path = "/uploads";       
        $this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}

(the check from source)

(i.e. check to see if the attribute is there and is a valid file).