php - Laravel 文件上传添加到集合移动文件在数字文件夹中

标签 php laravel file-upload laravel-5

我制作了一个表格,您可以在其中创建产品并为该产品上传多张图片。一切正常,除了文件被移动到一个子文件夹中(对应于数据库中 Media 表中的 id)

enter image description here enter image description here

我的 Controller :

    public function store(Request $request)
{
    $product = new Product;
    [...]
    $product->save();

    foreach ($request->get('product_categories') as $category_id) {
        $product->categories()->attach($category_id);
    }

    $files = $request->file('product_images');
    foreach($files as $file) {
        $product->addMedia($file)->toCollection('images');
    }
    return view('dashboard/create_product')->with('success', 1)->with('categories', Category::get());
}

产品模型

class Product extends Model implements HasMedia
{
   use HasMediaTrait, SoftDeletes;
   public function images() { 
      return $this->hasMany("App/Images");
   }

   public function categories()
   {
      return $this->belongsToMany("App\Category", 'category_product',    'product_id', 'category_id');
   }

   public function inventory()
   {
       return $this->hasOne('App\Inventory');
   }

   /**
    * The attributes that should be mutated to dates.
    *
    * @var array
    */
   protected $dates = ['deleted_at'];
}

为什么它被移动到一个子文件夹中?我该如何防止这种情况?

最佳答案

看起来文件是由这个方法处理的 ->addMedia()

这来自一些我不太了解的 laravel 包,但你应该查看包的文档,了解存储文件的默认路径。

检查一下:http://medialibrary.spatie.be/v3/advanced-usage/using-a-custom-directory-structure/

关于php - Laravel 文件上传添加到集合移动文件在数字文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35078204/

相关文章:

php - 当列包含重复值时如何使用索引优化 mySQL 查询

php - 如何返回 id 等于其他具有 Laravel 关系的表上的 id 的模型行?

laravel - 目标组 443 给出 Health checks failed with these codes : [502]

c# - 我可以从 Winforms 应用程序同时将文件和模型上传到 MVC 4 操作吗?

java - 文件未上传到服务器

php - 通过 PHP 和 CURL 使用 Mailgun 时,PHPMailer 的 AddStringAttachment 的等效项是什么?

php - Mysql:根据最小数量删除重复记录

php - 如何使用一对多关系在 Laravel 中保存选定列表?

java - REST 方法调用时上传的文件已损坏

PHP 从数据库中提取数据时将所有非英文字符替换为 null