I have a problem,
I want my route /browse to both display some elements and check for one condition by a written function, these are two separate functions, but I want them to be running on one route, is it even possible?
web.php
Route::get('/browse',[PlantsController::class, 'browse'])->name('browse');
Route::get('check',[PlantsController::class, 'checkForWatering'])->name('check');
I tried doing something like this:
public function browse()
{
$this->checkForWatering();
$this->displayPlants();
}
But in result it's a blank white page.
Here are my functions:
public function checkForWatering()
{
$all = DB::table('plants')
->select('*')
->get();
foreach ($all as $result)
{
$now = Carbon::now();
$nextWatering = (new Carbon ($result->watered_at))
->addDays($result->watering_frequency);
$daysPast = $nextWatering->diffInDays($now);
$query = DB::table('plants')
->where('watering_frequency', '>=', $daysPast)
->get();
$query->isEmpty() ? $result = true : $result = false;
return redirect()->route('browse')->with('result',$result);
}
}
public function displayPlants(){
$now = new Carbon();
$plants = DB::table('plants')
->where('user_id', '=', auth()->id())
->orderBy('watered_at', 'desc')
->get();
foreach ($plants as $plant)
{
$plant->watered_at = Carbon::parse($plant->watered_at)
->diffForHumans();
$plant->fertilized_at = Carbon::parse($plant->fertilized_at)
->diffForHumans();
}
return view('browse')->with('plants',$plants);
}