Example #1
0
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $rules = array('category' => 'required|Integer', 'condition' => 'required|Integer', 'name' => 'required', 'description' => 'required', 'donor' => 'required|Integer', 'orignal_price' => 'required|Numeric', 'price' => 'required|Numeric', 'image' => 'required', 'attributes' => 'required');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return $this->response(array('statusCode' => 400, 'statusDescription' => 'Bad Request', 'errors' => $validator->messages()->toJson()));
     } else {
         $iGetInstitute = Donor::where('user_id', Input::get('donor'))->first();
         $product = new Product(array('products_price' => Input::get('price'), 'products_orginal_price' => Input::get('orignal_price'), 'brand' => Input::get('brand', NULL), 'products_status' => Input::get('products_status', 0), 'products_donar' => Input::get('donor'), 'condition' => Input::get('condition'), 'products_image' => Input::get('image'), 'gender' => Input::get('gender'), 'age_range' => Input::get('age_range'), 'freshly_faved' => Input::get('freshly_faved'), 'collections_id' => Input::get('collections_id'), 'seasons_id' => Input::get('seasons_id'), 'types_id' => Input::get('types_id'), 'institution_id' => $iGetInstitute->institution_id));
         $product->save();
         $category = Categories::find(Input::get('category'));
         $product->productCategory()->save($category);
         if (Input::has('tags')) {
             $inptags = Input::get("tags");
             $tags = explode(",", $inptags);
             foreach ($tags as $key => $value) {
                 $ctag = Tags::find($value);
                 if ($ctag) {
                     $product->productTags()->save($ctag);
                 }
             }
         }
         $attributes = json_decode(Input::get("attributes"), TRUE);
         //$attribs = array();
         foreach ($attributes as $key => $value) {
             $opt_values = explode(",", $value);
             foreach ($opt_values as $optval) {
                 //echo $key." ".$optval."<br/>";
                 $attribute = new ProductsAttributes(array('options_id' => $key, 'options_values_id' => $optval));
                 $product->productsAttributes()->save($attribute);
             }
         }
         $description = new ProductsDescription(array("products_name" => Input::get("name"), "products_description" => Input::get("description")));
         $product->productsDescription()->save($description);
         return $this->response(array('statusCode' => 100, 'statusDescription' => 'Success'));
     }
 }