我创建了一个表单,您可以在该表单中创建产品并上传多个产品图像。一切都很好,除了文件被移动到子文件夹(对应于数据库中Media表中的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'];
}
为什么它被移动到了子文件夹中?我该如何防止这种情况发生?