cch
cch

Reputation: 3386

Laravel - Pass variable from a function to another

Is this the correct way to pass the id from a function to another ?

return Redirect::to('uploads/show', $id);

or should I use

return Redirect::to('uploads/show')->with($id);

the function that I am trying to pass the id to

public function getShow($id)
{
  $entryid = $this->upload->findOrFail($id);

  $this->layout->content = View::make('uploads.show', compact('entryid'));
}

The error i get is

Missing argument 1 for UploadsController::getShow()

Upvotes: 2

Views: 2794

Answers (2)

alexrussell
alexrussell

Reputation: 14202

If you want to do it as correctly as possible you need to craft your URL properly. The previous answer which uses string concatenation isn't wrong, but a better way might be:

return Redirect::to(URL::to('uploads/show', [$id]));

You will find though that as you get more routes, and especially the first time you decide to rework your routing convention, that using named routes (or at least controller-based routing with Route::controller) will probably serve you better. In this case it's a similar situation:

// routes.php
Route::get('uploads/show/{id}', ['as' => showUpload', 'uses' => 'Controller@action']);

// redirect:
return Redirect::route('showUpload', [$id]);

Also notice how you can pass the params directly in here (rather than using URL::to()) as using a named route isn't a simple string route like above.

Similarly, if you were using controller-based routing:

// routes.php
Route::Controller('uploads', 'Controller'); // I don't use this, syntax is like this

// redirect:
return Redirect::action('Controller@getShow', [$id]); // again I don't use this, but it's similar

Upvotes: 2

Tom
Tom

Reputation: 3664

When using Redirect you should do:

return Redirect::to('uploads/show/'.$id);

to pass variable to controller though router

Upvotes: 4

Related Questions