Triumfator's avatar

Best practice for passing id values between different controllers

Let's say I have an ItemController with CRUD functions. Edit blade view has 2 forms, one for editing textual input for all item input fields, and another form that handles multiple image uploads by a separate ImageController that writes image file name along with parent item_id to images database table.

I wasn't sure how to pass $item->id value to the ImageController correctly. Here's what I've done and it's working....

Put a hidden field in the form that handles image uploads.

<input type="hidden" name="item_id" value="{{ $item->id }}">

In the ImageController I'm retrieving this value with this function.

$item_id = $request->input('item_id');

Is this a correct method for doing this? Or is it ghetto? Are there better ways of doing it?

0 likes
1 reply
Nakov's avatar

That's one way to go about it. But another more of a CRUD approach would be to pass it through the URL. So for example in your URL for the images you can have {item}/images where item is the ID of the item for which you upload the images.

Then in your controller action you can bind it directly.. For example:

public function store(Request $request, Item $item)
{
 // Your code here
}

Please or to participate in this conversation.