I'm trying to call a controller method from a form object, to increment a given item.
The problem is that when adding the parameter, the form action will add a question mark, instead of a slash.
<form method="POST" action="http://localhost/admin/pages?1">
How am I to define the parameter?
{!! Form::open([
'action'=>['Admin\\PagesController@increment', $item->id],
'style' => 'display:inline'
]) !!}
{!! Form::submit('Move Up', ['class' => 'btn btn-danger btn-xs']) !!}
{!! Form::close() !!}
2 Answers 2
In you code sample, you are sending the item id as a HTTP GET parameter. You can access the item id in your controller by giving a name to the parameter as follows.
{!! Form::open([
'action'=>['Admin\\PagesController@increment','itemId='.$item->id],
'style' => 'display:inline'
]) !!}
Then access the item id in your controller by
Input:get('itemId')
Sign up to request clarification or add additional context in comments.
3 Comments
Zalon
Using the first option i get this error: Action App\Http\Controllers\/admin/pages/<?php echo e($item->id); ?> not defined Second options gives me an ErrorException: Array to string conversion
madawa
@Zalon it seems that you cannot pass parameters with post requests. So you need to choose the second method. I edited the code. Refer this answer: stackoverflow.com/questions/31872422/…
Zalon
I played around with it a bit, I ended up getting it to work by using: 'url' => ['admin/pages/'.$item->id.'/increment'], instead of action. Thanks a lot for your help
Your function shhould looks like this
public function increment($id)
{
//your code;
}
And your route should have id in it with post request
Route::post('increment/{id}','PagesController@increment');
Comments
lang-php
Admin\PagesController@increment?