1

I am trying to figure out how to access two (or more) parameters passed to a Laravel controller. I know how to create the route, and the URL is created correctly, but then I can only access the first passed parameter in my controller.

Route:

Route::get('managers/{id}/{parameter2}', array('as'=>'dosomething', 'uses'=> 'ManagersController@dosomething'));

where the first parameter is obviously the $id for managers, and the second parameters is to be processed by the controller.

View:

<a href="{{ URL::route('do something',array($manager->id,'parameter2') }}" class="styleClass">Do Something</a>

generates the URL:

http://domain/managers/1/2

where 1 is easily accessed as the $id for managers, but when I try to access the 2nd parameter "2" using $parameter2, e.g. using a simple return: "id=$id and parameter2=$parameter2" statement, I get an "unidentified variable: $parameter2" error.

What am I doing wrong?

Is there a better way to pass multiple parameters? I'm especially asking the "better way?" question because what I want to do is use the 2nd parameter to change a value in a database table, and using a 'get' method, somebody could change the parameter value in the URL and therefore cause mischief. Must I use a 'post' method? I'd love to be able to use a link, since that works much better with the design of my application.

Thanks!


I was asked to include the controller, which I'm happy to do. Initially, just for testing, as I mentioned, my controller was a simple return to display the values of the two passed parameters. But here is what I want to be able to do, including the actual name of the function ("update_group" rather than "dosomething") --

ManagersController:

public function update_group($id)
{
    DB::table('groups')->where('id','=',$parameter2)->update(array('manager_id'=>$id));
    return Redirect::route('managers.show', array('id'=>$id));
}

The update table works perfectly if I replace $parameter2 with an actual value, so that syntax is fine. The issue is that Laravel says that $parameter2 is an undefined variable, despite the fact that the URL contains the value of $parameter2 as you can see above.

And since it occurs to me that the answer to this may involve adding a function to the Manager model, here is the current

Manager.php

class Manager extends Eloquent {

protected $table = 'managers'; ... (mutator and error functions)

}
2
  • Post your controller dosomething function Commented Jul 28, 2014 at 11:47
  • I've added the controller, and the model. See above. Thanks. Commented Jul 28, 2014 at 16:31

2 Answers 2

2

Just change

public function update_group($id)

to

public function update_group($id, $parameter2)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Of course! (Silly me!) I really appreciate it.
1

All looks ok in your route. Seeing the controller code would help, but likely, you may not have a second parameter in your controller's dosomething() method.

public function dosomething($id, $parameter2){
    var_dump($id).'<br />';
    var_dump($paremter2);
}

If that isn't the case, you can try dumping it from the route's callback to further diagnose.

Route::get('managers/{id}/{parameter2}', function($id, $parameter2)
{
    var_dump($id).'<br />';
    var_dump($paremter2);
});

Depending on your use case, you can pass them in a query string like so: but it isn't really the 'best way', unless you're doing something like building an API that won't use the same variables in the same order all the time.

/managers?id=1&paramter2=secondParameter

var_dump(Request::query('id')).'<br />';
var_dump(Request::query('paramter2'));

2 Comments

Thanks. I've added the controller to my original question above. The var_dump gives the same result as a return that attempts to display the parameter values -- each time, I get an "Undefined variable: parameter2" error. I get the same error if I try to use a query string. The Route is back to the controller, so that doesn't help. As I mentioned, the URL is constructed correctly, it's just that I'm unable to access the 2nd parameter within the controller. There must be a way to do that.
you're route is passing $parameter2, but your controller isn't accepting it. Try changing your method declaration to public function update_group($id, $parameter2) That should do the trick.

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.