1

What am trying to do is to build a laravel axios routes which have query parameters that is

a sample request looks like

 axios.get("/user-management/permissions?role=" + this.role)

so the aboe generates

localhost:8000/user-management/permissions?role=admin"//role value changes

Now am stuck at setting the actual routes in the laravel routes

I have the following

Route::get('permissions/{role}', "PermissionsController@PermissionRole");

THe above route is never executed.How do i go about setting up my laravel routes to have the query string parameter

0

2 Answers 2

4

I think you misunderstood:

The route Route::get('permissions/{role}', "PermissionsController@PermissionRole"); will respond to requests of the form /user-management/permissions/admin

You don't need to specify the expected query string. You need to have this route:

Route::get('permissions', "PermissionsController@PermissionRole");

Then in your controller:

function PermissionRole(Request $request) {
       $role= $request->get("role"); //admin ?
}

If you want to add the role as part of the URL you can do:

Route::get('permissions/{role}', "PermissionsController@PermissionRole");

Then you can access the role in the action:

function PermissionRole(Request $request, $role) {
      //$role variable name matches the route name
}

However you can also make it mandatory by using validation:

function PermissionRole(Request $request) {
       $this->validate($request->all(), [ 
              "role" => "required|in:admin,user" //example
       ]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

okay, i see my mistake but is it possible to build routes with parameters am trying to build an api where people will pass in query parameters eg: ?role=""?date=""?newitem=""
@GEOFFREYMWANGI not with query string parameters, but you can perform Validation on the Request object or even override it with your own route specific class (using Form validation
thanks also based on the way youve put it i cannot enforce the role to be mandatory so adding adding /{role} makes it mandatory but now i cannot fetch the mandatory parameter ow do i go about this.
i see where its going wrong to fetch ive had to change $request->get("role") to $request->route("role"). Update your answer on the same.
Updated with an alternative way. But I stil l think validation is better. There's a lot more rules you can impose other than required. You can validate to only accept certain values or number etc.
1

route:

Route::get('permissions/{role}', "PermissionsController@PermissionRole");

Is never executed because you should have link like this:

localhost:8000/user-management/permissions/admin  //admin is role

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.