phplaravelbackendlaravel-routingreact-routing

What is the correct method to pass parameter to Laravel controller for filtering data?


This is my code of route for getting data from Laravel backend.

Route::get('/get/card',[CardController::class,'getCardList'])->name('card.list');

I call it like below, http://127.0.0.1:8000/get/card

Controller code

public function getCardList()
{
   //code goes here
}

The above code is working fine. I'm trying to add a parameter for adding filtration as follows;

Route::get('/get/card{treeItemID?}',[CardController::class,'getCardList'])->name('card.list');

public function getCardList($treeItemID)
{
}

http://127.0.0.1:8000/get/card?treeItemID=1

But, I'm getting the error "Too few arguments to function app\Http\Controllers\CardController::getCardList()..."

Can anyone notice what's wrong with my code that gives the above error when the parameter is added? Any help would be highly appreciated.


Solution

  • if you want to get data like below url, please replace your route and method like below and check again. http://127.0.0.1:8000/get/card?treeItemID=1

    Route::get('/get/card',[CardController::class,'getCardList'])->name('card.list');
    
    public function getCardList(Request $request){
    
      $treeItemID = $request->input('treeItemID');
    
      return $treeItemID;
    
    }