简体   繁体   English

Laravel创建自定义方法以获取表单数据

[英]Laravel create custom method to get form data

How do I make custom method to get form data? 如何制作自定义方法以获取表单数据? I want this method same with Laravel update method with parameters request and id. 我希望此方法与带有参数request和id的Laravel更新方法相同。 I try this but get error. 我尝试这样做,但出现错误。

In controller 在控制器中

public function updatePassword(Request $request, int $id) {
   dd($request->all());
}

In route 在途中

Route::post('staffs/{id}/upassword', 'Admin\StaffController@updatePassword')->name('admin.staffs.upassword');

In blade file 在刀片文件中

<form method="post" accept-charset="utf-8" action="{{ action('Admin\StaffController@updatePassword', ['id' => $staff_id]) }}">
    <div class="row">
        <div class="col-md-3">
            <div class="form-group">
                <label class="control-label" for="password">New Password</label>
                <input class="form-control" name="password" type="password">
            </div>
       </div>
   </div>
   <div class="row">
       <div class="col-md-3">
           <div class="form-group">
               <label class="control-label" for="password_confirmation">Confirm New Password</label>
               <input class="form-control" name="password_confirmation" type="password">
           </div>
       </div>
  </div>

  <input class="btn btn-primary" type="submit">
</form>

I am using Laravel 5.4. 我正在使用Laravel 5.4。

here are some stuff to fix: 这里有一些要修复的东西:

  • First in the tag you can set the action to : action="route('admin.staffs.upassword', $staff_id)" since it's easier to write and since you already gave the route a name, so why not using it ;) 首先,您可以在标记中将操作设置为: action="route('admin.staffs.upassword', $staff_id)"因为它更容易编写,并且已经为路由指定了名称,所以为什么不使用它;)
  • Second add { {csrf_field() }} right before your form closing tag </form> 其次,在表单结束标记</form>之前添加{ {csrf_field() }}

what error are you getting? 你遇到了什么错误? the error is probably because you are not using {{csrf_field()}} after the form declaration, it is needed so that laravel can validate the request. 该错误可能是因为您在表单声明后没有使用{{csrf_field()}} ,所以它是必需的,以便laravel可以验证请求。 if you want to get the data from the form you can use: 如果要从表单中获取数据,可以使用:

$request->get('inputname');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM