/** * Execute the console command. * * @return mixed */ public function handle() { $products = Product::all(); $userProducts = User_Product::all(); foreach ($userProducts as $up) { $userPrice = UserPrice::where('product_id', $up->products_id)->first(); $user = User::where('id', '=', $userPrice->user_id)->first(); $product = Product::where('id', '=', $up->products_id)->first(); $targetPrice = $userPrice->target_price; $checkUrl = new CheckUrl(); $provider = $checkUrl->checkurl($product->url); $priceFinder = new PriceFinder(); $productInfo = $priceFinder->{$provider}($product->url); if ($productInfo['price'] <= $targetPrice) { $data = array('price' => 'The price is now lower check it please' . ' ' . 'The link of this product is' . ' ' . $product->url); if ($user) { $subject = 'Track Your Price'; Mail::send('mail.price-notification', $data, function ($message) use($subject, $user) { $message->to($user->email, $user->name)->subject($subject); }); } } } }
/** * @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); }