public function getFertilizeHistory(Request $request) { $plant = Plant::findOrFail($request->get('plant_id')); // set moisture / watering history according to selected week $lastPlantMoisture = $plant->moisture()->orderBy('date', 'DESC')->first(); if ($request->has('week')) { $selectedWeekWatering = $request->get('week'); } else { if (isset($lastPlantMoisture)) { $selectedWeekWatering = $lastPlantMoisture->date->weekOfYear; } else { $selectedWeekWatering = null; } } $allPlantMoisture = $plant->moisture; $plantMoisture = []; foreach ($allPlantMoisture as $moisture) { if ($moisture->date->weekOfYear == $selectedWeekWatering) { array_push($plantMoisture, $moisture->value); } } $firstweek = $plant->moisture()->first() ? $plant->moisture()->first()->date->weekOfYear : null; $plantMoistureLength = count($plantMoisture); if ($firstweek && $selectedWeekWatering == $firstweek && $plantMoistureLength < 7) { for ($i = 0; $i < 7 - $plantMoistureLength; $i++) { array_unshift($plantMoisture, $plantMoisture[0]); } // double last value array_push($plantMoisture, $plantMoisture[count($plantMoisture) - 1]); } return $plantMoisture; }
/** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { $plant = Plant::findOrFail(Route::input('id')); // authorize when the user is the owner of the plant return Auth::user()->id == $plant->users()->wherePivot('isOwner', true)->first()->id; }