Search code examples
laravellaravel-bladelaravel-routing

Cannot get id of subscription in request


I'm trying to get the id of the selected subscription and pass it to the controller, but for some reason when I dd() or print_r() the id in the controller, it returns the same id regardless of which subscription I click.

Regardless if i click the first '+' button or the second one it always returns the same id when I print_r() it.

enter image description here

Array ( [_token] => iFimNDCv0q4rq7OmmUWGN8SGr2Bq0brsiRMLIzBD [subscription] => 2 )

Even when I click the first one, which should have '1' as the id, it still returns '2'.

This is my route for it in web.php

Route::post('/transaction/store', 'TransactionController@store')->name('transaction.store');

This is the function in the controller

public function store(Request $request)
{
    // continue here
    print_r($request->input());
}

And this is the form tag that's responsible for send the id (which is inside a foreach statement in blade)

<form id="batch" action="{{route('transaction.store')}}" method="POST">
     @csrf
     <input type="hidden" name="subscription" value="{{$subscription->id}}" form="batch">
     <button type="submit" class="btn btn-sm btn-outline-dark" form="batch">+</button>
</form>

Thanks very much for any help!


Solution

  • I just had to make the form id dynamic!

    <form id="batch-{{$subscription->id}}" action="{{route('transaction.store')}}" method="POST">
         @csrf
         <input type="hidden" name="subscription" value="{{$subscription->id}}" form="batch-{{$subscription->id}}">
         <button type="submit" class="btn btn-sm btn-outline-dark" form="batch-{{$subscription->id}}">+</button>
     </form>