1

I would like to pass the id of an object to a controller in laravel. Basically I've a page that displays the information relating to a book. The user has the option to add themes to that book. The line below is the link they click in order to open the create theme form:

<a href="{{action('ThemeController@create')}}" class="btn btn-primary">Add Theme</a> 

and the ThemeController method:

public function create(){ $books = Book::all(); return View::make('theme.create', compact('books')); } 

Currently this brings up a Form with a list of all of my books in a dropdown. I would rather just pass the id of the book the user was on prior to being brought to this form and use that instead. Is there any way I can do that? I've also included my routes.php file:

Route::model('book', 'Book'); //Show pages Route::get('/books', 'BookController@index'); Route::get('book/create', 'BookController@create'); Route::get('book/edit/{book}', 'BookController@edit'); Route::get('book/delete/{book}', 'BookController@delete'); Route::get('book/view/{book}', 'BookController@view'); ////Handle form submissions Route::post('book/create', 'BookController@handleCreate'); Route::post('book/edit', 'BookController@handleEdit'); Route::post('book/delete', 'BookController@handleDelete'); //Themes Route::model('theme', 'Theme'); Route::get('/themes', 'ThemeController@index'); Route::get('theme/create', 'ThemeController@create'); Route::get('theme/edit/{book}', 'ThemeController@edit'); Route::get('theme/delete/{book}', 'ThemeController@delete'); Route::get('theme/view/{book}', 'ThemeController@view'); Route::post('theme/create', 'ThemeController@handleCreate'); Route::post('theme/edit', 'ThemeController@handleEdit'); Route::post('theme/delete', 'ThemeController@handleDelete'); 
4
  • I'm assuming you did not use Laravel's Artisan Controller Make function? Commented Aug 14, 2014 at 7:08
  • I didn't use it for the Conrollers but had been using it for the Migrations Commented Aug 14, 2014 at 7:10
  • 1
    Aah okay, well, I would recommend using Resource Controllers.. You're pretty much using that structure already. However, Resource Controllers will simplify your Route structure, by quite a lot :). Commented Aug 14, 2014 at 7:17
  • Unless of course you're not building a RESTful application and would prefer not to use Put and Delete at all. Then I understand. Commented Aug 14, 2014 at 7:18

1 Answer 1

4

You can still use normal $_GET methodology. On your route:
book/create You could simply request. book/create?book_id=xxyy Then in your controller create function

public function create(){ $bookId = Input::get('book_id'); $books = Book::all()->where('book_id' => $bookId); return View::make('theme.create', compact('books')); } 

PLEASE NOTE: I haven't used Laravel in a while, don't trust that elequont query :P.. I was just putting a theoretical "where" in there.

And here http://laravel.com/docs/requests#basic-input, you will note this:

"You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs."

So whether you're using get, put, post or delete, whatever you wrapped in your header, you will be able to find using Input Class.

EDIT
Just needed to add, that you won't need to modify your routes at all. This is just a Simple GET parameter in a GET request. Just like in any other application.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.