Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

learn4u's avatar

Check Empty Paramater

Hello Guys ,

I have a question please , maybe I'am doing something wrong so I need your help !

Well , I want to check if the paramater is empty in my URL or not !

This is my route :

Route::get('tasks/category/{caid?}/' , 'TaskController@getTaskByCategory');

This is my function :

public function getTaskByCategory(Task $task , $caid = null){
        if ($caid == NULL || $caid == '') {
            echo "test" ; 
        }
        if(!preg_match("/^[0-9]*$/",$caid)){
            return response()->json(['status' => 'error', 'message' => 'Method not Allowed.'], 405);
        }else{
            $tasks = Task::where('category_id' ,  $caid)->paginate(10) ; 
            return TaskResource::collection($tasks) ;   
        } 
    }        

the first condition doesn't work ? Why ? the rest are fine .

I want to see when I visit : tasks/category/ :

test

Thank you

0 likes
2 replies
manelgavalda's avatar
Level 50

The parameters of your controller don't match with your route parameters, so you are probably missing to pass the task to parameters list on your route definition.

Route::get('tasks/category/{task}/{caid?}' , 'TaskController@getTaskByCategory');

Or, you don't make use of your $task parameter, so maybe you dont wan't to pass it in your controller.

//routes/web.php
Route::get('tasks/category/{caid?}' , 'TaskController@getTaskByCategory');

// TaskController.php
public function getTaskByCategory($caid = null){
// 
}

Please or to participate in this conversation.