我制作了一个表单,您可以在其中创建产品并为该产品上传多个图像。一切正常,除了文件被移动到子文件夹(对应于数据库中的媒体表中的id)
我的控制器:
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'];
}
为什么它会移动到子文件夹中?我怎么能阻止这个?
答案 0 :(得分:1)
该文件似乎由此方法->addMedia()
这来自我不太了解的一些laravel包,但您应该查看包的文档,重新定位存储文件的默认路径。
检查出来:http://medialibrary.spatie.be/v3/advanced-usage/using-a-custom-directory-structure/