/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(StoreProductRequest $request)
 {
     $product = new Product();
     $product->title = $request->title;
     $product->abstract = $request->abstract;
     $product->content = $request->content;
     $product->prix = $request->prix;
     $product->status = $request->status;
     $product->category_id = $request->category_id;
     $image = new Image();
     $img_name = $request->file('image')->getClientOriginalName();
     $image->name = $img_name;
     $request->file('image')->move(public_path('images'), $img_name);
     $image->uri = 'images/' . $img_name;
     if ($image->save()) {
         $product->image_id = $image->id;
         $product->save();
         //gestion des tags
         if (isset($request->Etoile)) {
             $product_tag = new ProductTag();
             $product_tag->product_id = $product->id;
             $product_tag->tag_id = $request->Etoile;
             $product_tag->save();
         }
         if (isset($request->Espace)) {
             $product_tag = new ProductTag();
             $product_tag->product_id = $product->id;
             $product_tag->tag_id = $request->Espace;
             $product_tag->save();
         }
         if (isset($request->Galaxie)) {
             $product_tag = new ProductTag();
             $product_tag->product_id = $product->id;
             $product_tag->tag_id = $request->Galaxie;
             $product_tag->save();
         }
         if (isset($request->Empire)) {
             $product_tag = new ProductTag();
             $product_tag->product_id = $product->id;
             $product_tag->tag_id = $request->Empire;
             $product_tag->save();
         }
         if (isset($request->Alliance)) {
             $product_tag = new ProductTag();
             $product_tag->product_id = $product->id;
             $product_tag->tag_id = $request->Alliance;
             $product_tag->save();
         }
         \Session::flash('message', 'Produit bien créé en BDD.');
     } else {
         \Session::flash('message', 'Probleme lors de l\'acces à la BDD. Merci de réessayer.');
     }
     return redirect('admin/product-form');
 }
示例#2
0
 /**
  * Store a newly created resource in storage.
  *
  * @param StoreProductRequest|\Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function store(StoreProductRequest $request)
 {
     $input = $request->input();
     $input = $this->setImage($request->file('image'), $input);
     Product::create($input);
     return redirect()->action('ProductController@create');
 }
 public function update(StoreProductRequest $request, $productId)
 {
     $input = $request->all();
     unset($input['picture']);
     $destinationPath = public_path('uploads');
     $fileName = uniqid();
     if ($request->hasFile('picture')) {
         if ($request->file('picture')->isValid()) {
             $request->file('picture')->move($destinationPath, $fileName);
         }
     }
     $input['picture'] = url('uploads') . '/' . $fileName;
     $input['slug'] = str_replace(' ', '_', strtolower($input['name']));
     $product = Product::where('id', $productId)->update($input);
     return $this->createResponse($product);
 }