jeudi 28 janvier 2016

Laravel file upload add to collection move file in numeric folder

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)

enter image description here

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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire