Ejemplo n.º 1
0
 public function getStore($id)
 {
     $store = Store::select('stores.*')->join('products', 'products.store_id', '=', 'stores.id')->where('products.id', $id)->first();
     return $store->name;
 }
 /**
  * @param Request $request
  * @return string
  */
 public function findprice(Request $request)
 {
     $inputUrl = $request->get('url');
     $checkUrl = new CheckUrl();
     $provider = $checkUrl->checkurl($inputUrl);
     $priceFinder = new PriceFinder();
     $productInfo = $priceFinder->{$provider}($inputUrl);
     //insert Data into stores table
     $store = new Store();
     $store->name = $provider;
     $store->save();
     $storeGet = Store::orderBy('id', 'desc')->first()->toArray();
     //gets last inserted row
     $storeId = $storeGet['id'];
     //gets store id of last inserted row
     //insert Data into products table
     //        $alreadyProduct = Product::select('products.*')
     //                                ->join('user_product', 'user_product.products_id', '=', 'products.id')
     //                                ->join('users', 'users.id', '=', 'user_product.user_id')
     //                                ->where('user_product.user_id', Auth::user()->id)
     //                                ->where('url', $inputUrl)
     //                                ->first();
     $alreadyProduct = Product::where('url', $inputUrl)->first();
     if ($alreadyProduct) {
         $alreadyProduct->name = $productInfo['name'];
         $alreadyProduct->image = $productInfo['image'];
         $alreadyProduct->save();
     } else {
         $product = new Product();
         $product->store_id = $storeId;
         $product->name = $productInfo['name'];
         $product->url = $inputUrl;
         $product->image = $productInfo['image'];
         $product->save();
     }
     //insert data into price_history table
     if ($alreadyProduct) {
         $alreadyPriceHistory = Price_History::where('products_id', $alreadyProduct->id)->first();
         $alreadyPriceHistory->price = $productInfo['price'];
         $alreadyPriceHistory->save();
     } else {
         $price_history = new Price_History();
         $price_history->products_id = $product->id;
         $price_history->price = $productInfo['price'];
         $price_history->save();
     }
     $userId = Auth::user()->id;
     $user_product = new User_Product();
     $user_product->user_id = $userId;
     $user_product->products_id = $alreadyProduct ? $alreadyProduct->id : $product->id;
     $user_product->save();
     if ($alreadyProduct) {
         return Redirect::route('target-price', $alreadyProduct->id);
     }
     return Redirect::route('target-price', $product->id);
 }