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