/** * Display the specified resource. * * @param int $productId * @param Requests\ShowingSamplingsRequest $request * @return \Illuminate\Http\Response */ public function show($productId, ShowingSamplingsRequest $request) { Product::findOrFail($productId); // Just to throw a failure if the product is not found $samplingsQueryBuilder = Sampling::query(); $samplingsQueryBuilder->join('sensors', 'samplings.sensor_id', '=', 'sensors.id'); $samplingsQueryBuilder->join('products', 'sensors.product_id', '=', 'products.id'); $samplingsQueryBuilder->where('products.id', $productId); $limit = $request->has('limit') && $request->limit < 1000 ? $request->limit : 1000; $timeFiltersApplied = $this->filterResultsInTime($samplingsQueryBuilder, $request); if (!$timeFiltersApplied) { $samplingsQueryBuilder->whereDate('samplings.created_at', '=', Carbon::today()->toDateString()); } if ($request->generic_sensor_id) { $samplingsQueryBuilder->join('generic_sensors', 'sensors.generic_sensor_id', '=', 'generic_sensors.id'); $samplingsQueryBuilder->where('generic_sensors.id', $request->generic_sensor_id); } $samplingsQueryBuilder->orderBy('samplings.created_at', 'desc'); $samplingsQueryBuilder->take($limit); return $samplingsQueryBuilder->get(array('samplings.id', 'samplings.sensor_id', 'samplings.sampled', 'samplings.created_at')); }
/** * Display the specified resource. * * @param int $productId * @param Requests\ShowingSamplingsRequest $request * @return \Illuminate\Http\Response */ public function show($productId, ShowingSamplingsRequest $request) { $product = Product::join('locations', 'locations.product_id', '=', 'products.id')->where('locations.user_id', $this->guard->user()->getKey())->findOrFail($productId); // Just to throw a failure if the product is not found $samplingsQueryBuilder = Sampling::query(); $samplingsQueryBuilder->join('sensors', 'samplings.sensor_id', '=', 'sensors.id'); $samplingsQueryBuilder->join('generic_sensors', 'sensors.generic_sensor_id', '=', 'generic_sensors.id'); $samplingsQueryBuilder->join('products', 'sensors.product_id', '=', 'products.id'); $samplingsQueryBuilder->where('products.id', $productId); $samplingsQueryBuilder->orderBy('samplings.created_at', 'desc'); $limit = $request->has('limit') && $request->limit < 1000 ? $request->limit : 1000; if ($request->generic_sensor_id) { $samplingsQueryBuilder->where('generic_sensors.id', $request->generic_sensor_id); } $timeFiltersApplied = $this->filterResultsInTime($samplingsQueryBuilder, $request, $product); if (!$timeFiltersApplied) { $samplingsQueryBuilder->whereDate('samplings.created_at', '=', Carbon::today()->toDateString()); } $samplingsQueryBuilder->take($limit); // if ($timeFiltersApplied && array_has(["week", "month", "year"], $request->timeFilter)) { // return $samplingsQueryBuilder->selectRaw(" // samplings.sensor_id, // max(samplings.sampled), // generic_sensors.id as generic_sensor_id, // min(samplings.created_at) // "); // } return $samplingsQueryBuilder->get(array('samplings.id', 'samplings.sensor_id', 'generic_sensors.id as generic_sensor_id', 'samplings.sampled', 'samplings.created_at')); }