user2480176
user2480176

Reputation: 147

Pass variables to route with URL::to in Laravel

Here is how I am calling it in a create.blade.php of the same controller as the route I am trying to call:

{{ Form::open(['route' => 'myRoute']) }}
   <button type="submit" href="{{ URL::to('myRoute') }}" class="btn btn-danger btn-mini">Delete</button>
{{ Form::close() }}

The Route is:

Route::post('myRoute', ['as' => 'timeline.myRoute', 'uses' => 'TimelineController@myRoute']);

I want to pass an integer to the the route. I know that ->with() doesn't work like in View::make(). What is an efficient way to pass a variable into myRoute? Any help is appreciated.

Upvotes: 0

Views: 5920

Answers (3)

user2480176
user2480176

Reputation: 147

Jeemusu was 90% correct but forgot to specify a variable when opening a form.

What ultimately ended up working was:

{{ Form::open(array('route' => array('timeline.myRoute', $id))) }}
      <button type="submit" href="{{ URL::route('timeline.myRoute', array($id))     }}" class="btn btn-danger btn-mini">Delete</button>
{{ Form::close() }}

With the Route in Route.php:

Route::post('myRoute/{id}', ['as' => 'timeline.myRoute', 'uses' => 'TimelineController@myRoute']);

And the function in my controller:

class TimelineController extends BaseController
{
     public function myRoute($id) {
          return $id;
     }
}

Hope this helps anyone who had my problem.

Upvotes: 1

Eduardo Reveles
Eduardo Reveles

Reputation: 2175

Try to use URL::route('timeline.myRoute', array(1)); or even URL::to('myRoute', array(1));

Edit:

You route would be something like this:

Route::get('myRoute/{id}', ['as' => 'timeline.myRoute', 'uses' => 'TimelineController@myRoute']);

Then when you call to it with this:

echo URL::route('timeline.myRoute', array($id));

You can access in your controller:

class TimelineController extends BaseController
{
    public function myRoute($id) {
        echo $id;
    }
}

Upvotes: 0

Jeemusu
Jeemusu

Reputation: 10533

You can use route parameters to pass data to your controller from the url.

Say you had a url like http://yoursite.com/myRoute/id_number_here. Your routes and controller might look like this.

Route

Route::post('myRoute/{id}', ['as' => 'timeline.myRoute', 'uses' => 'TimelineController@myRoute']);

Controller

public function myRoute($id) {
    return $id;
}

Upvotes: 1

Related Questions