简体   繁体   中英

How can I execute 2 functions on 1 route in Laravel?

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);

}

You can do this by calling the other controller method and assigning the response to a variable before returning it.

public function browse() 
{
    $response = $this->checkForWatering();
    // Checks if the Response of checkForWatering exists
    return !empty($response) ? $response : $this->displayPlants();
}

While you can call two Controller methods within another Controller method. Ideally you'd want to break the logic of these out of the controllers and into their own classes.

Software architecture is very subjective but I recommend taking a look into some resources about Controllers and how some people recommend laying them out within applications, I believe this will help you with regards to bumping your head with issues like these later down the line like wanting to call the same bit of code within two Controllers.

You're not returning the view, change browse to:

public function browse() {
    $response = $this->checkForWatering();
    if (!empty($response)) {
         // If checkForWatering returned a response object, return it
         return $response;
    }
    return $this->displayPlants();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM