Laravel Backpack上传字段不是将图像移动到我的路径,而是它存储数据库的路径

时间:2018-02-02 14:00:29

标签: image laravel upload

当我使用laravel背包上传图像时,上传字段文件路径存储在数据库中,但图像不会移动到文件夹中。

这是我的控制器:

$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;
]);

这是我的模型:

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',
            ]

1 个答案:

答案 0 :(得分:1)

我解决了这个问题

ArticleCrudController:

...
     $this->crud->addField([ // image
            'label' => "Изображение",
            'name' => "images",
            'type' => 'image',
            'upload' => true,

            'crop' => true, 
            'aspect_ratio' => 1.5, 
            'prefix' => 'vector/upload/' 
        ]);
...

模型文章 ..

public function SetImagesAttribute($value)
{
    $attribute_name = "images";
    $disk = "uploads";
    $destination_path = "vector/upload/";

    // if the image was erased
    if ($value==null) {
        // delete the image from disk
        \Storage::disk($disk)->delete($this->{$attribute_name});

        // set null in the database column
        $this->attributes[$attribute_name] = null;
    }

    // if a base64 was sent, store it in the db
    if (starts_with($value, 'data:image'))
    {
        // 0. Make the image
        $image = \Image::make($value);
        // 1. Generate a filename.
        $filename = md5($value.time()).'.jpg';
        // 2. Store the image on disk.
        \Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
        // 3. Save the path to the database
        $this->attributes[$attribute_name] = /*$destination_path.'/'.*/$filename;
    }
}

...

并在config / filesystems.php中添加磁盘驱动程序

'disks' => [

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

最后,如果使用缓存,则必须运行命令

php artisan config:cache

并查看bootstrap \ cache \ config.php应该有一个条目

'filesystems' => 
  array (
    'default' => 'local',
    'cloud' => 's3',
    'disks' => 
   ..
      'uploads' => 
      array (
        'driver' => 'local',
        'root' => 'yorsitename.com\\public',
        'url' => '/vector/upload',
        'visibility' => 'public',
      ),
    ),