Reputation: 1766
I made a form where you can create a product and upload multiple image for that product. Everything works fine, except the file is being moved into a subfolder (corresponding to the id in the Media table in the database)
My 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());
}
The Product Model
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'];
}
Why is it moved in a subfolder ? how can i prevent this?
Upvotes: 3
Views: 1018
Reputation: 430
It looks like the file is processed by this method ->addMedia()
This comes from some laravel package I don't really know about, but you should look at the package's documentation regargind the default path where your files are stored.
Check this out : http://medialibrary.spatie.be/v3/advanced-usage/using-a-custom-directory-structure/
Upvotes: 2