![](/img/trans.png)
[英]How do I pass variables from the controller to the view in laravel react?
[英]Laravel: how do I pass a value from a form to a controller?
我有一個表格:
{ Form::open(array('action' => 'RatesController@postUserRate', $client->id)) }}
{{ Form::text('rate', '', array('placeholder' => 'Enter new custom client rate...')) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
如何通過表單將$ client-> id值傳遞給我的控制器方法?
我目前有一個控制器方法,如下所示:
public function postUserRate($id)
{
$currentUser = User::find(Sentry::getUser()->id);
$userRate = DB::table('users_rates')->where('user_id', $currentUser->id)->where('client_id', $id)->pluck('rate');
if(is_null($userRate))
{
...
}else{
....
}
}
並且錯誤日志顯示“對於RatesController :: postUserRate()缺少參數1”
關於如何將這個$ client-> id傳遞給我的控制器的任何想法,所以我可以按照我想要的那樣使用它?
將{{ Form::hidden('id', $client->id) }}
到表單中。 然后,當它被發布時,您可以使用Input::get('id')
每平常獲取其值。
另外,刪除postUserRate
方法的參數。
你只需使用:
Form::open(array('action' => array('Controller@method', $user->id)))
$user->id
作為參數傳遞給方法method
,最后一個也應該接收一個參數,如下所示: method($userId)
來源:Laravel 文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.