首页 > 解决方案 > 表单数据未使用 laravel 5.4 版本提交到数据库

问题描述

我试图简单地添加我的表单数据,但我不知道我在哪里错了我的 html 视图:

<form action="{{url('add/talent')}}" method="post" role="form">

    {!! csrf_field() !!}
    <div class="row">
        <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <div class="form-group">
                <label for="">First Name</label>
                <input type="text" name="first_name" class="form-control" id="" placeholder="">
            </div>
        </div>
        <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <div class="form-group">
                <label for="">Middle Name</label>
                <input type="text"name="middle_name" class="form-control" id="" placeholder="">
            </div>
        </div>
        <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <div class="form-group">
                <label for="">Last Name</label>
                <input type="text"name="last_name" class="form-control" id="" placeholder="">
            </div>
        </div>
    </div>

    <div class="form-group">
        <label for="">Email Address</label>
        <input type="text" name="email_address" id="input" class="form-control" value="" required="required" pattern="" title="">
    </div>

    <div class="form-group">
        <label for="">Profile Summary</label>
        <textarea name="" id="input" name="profile_summary" class="form-control" rows="3" required="required"></textarea>
    </div>

    <p class="bold m-b-10">Open for following type</p>

    <div class="check-box-group">
        <label class="checkbox-inline">
            <input type="checkbox" id="inlineCheckbox1" value="option1"> Freelance Project
        </label>
        <label class="checkbox-inline">
            <input type="checkbox" id="inlineCheckbox2" value="option2"> Contract Project
        </label>
        <label class="checkbox-inline">
            <input type="checkbox" id="inlineCheckbox3" value="option3"> Part Time Hire
        </label>
        <label class="checkbox-inline">
            <input type="checkbox" id="inlineCheckbox3" value="option3"> Direct Hire ( Full time )
        </label>
        <label class="checkbox-inline">
            <input type="checkbox" id="inlineCheckbox3" value="option3"> SOW
        </label>
    </div>

    <div class="m-t-20 m-b-20">
        <a href="add-talent-2" type="submit" class="btn btn-success">Continue</a>
    </div>

</form>

我的控制器:

public function addTalent(Request $request)
{
    $rules = array(
        'first_name'         => 'required',
        'middle_name'        => 'required',
        'last_name'          => 'required',
        'email_address'      => 'required',
        'profile_summary'    => 'required',
    );
    $validator = \Illuminate\Support\Facades\Validator::make(Input::all(), $rules);

    if ($validator->fails()) {
        return Redirect::to('add-talent-1')
            ->withErrors($validator);
    } else {
        // store
        $talent = new Talent();
        $talent->first_name = Input::get('first_name');
        $talent->middle_name = Input::get('middle_name');
        $talent->last_name = Input::get('last_name');
        $talent->email_address = Input::get('email_address');
        $talent->profile_summary = Input::get('profile_summary');
        $talent->save();
    }
}

和我的模型:

类人才扩展模型 { 保护 $table = 'add_talent_1';

   protected $fillable = ['first_name','middle_name','last_name','email_address','profile_summary'];

和我的路线/web.php

     Route::post('add/talent', 'AddTalent@addTalent');

如果我在 routes/api.php usig postman 下测试我的路由并点击我的 post 请求,它会使用 url 成功地将数据添加到数据库中:http: //127.0.0.1 :8000/api/add/talent

但是,如果我删除了 api 并使用 url http://127.0.0.1:8000/add/talent简单地点击请求,它会在 VerifyCsrfToken.php 第 68 行中显示 TokenMismatchException 我不知道我在哪里做错了为什么我的表单没有提交我的数据

任何帮助将不胜感激!

标签: phphtmlformstokenlaravel-5.4

解决方案


默认情况下,Laravel 在 Web 路由中使用 csrf 中间件来检查和验证请求是否具有有效的 csrf 令牌。这不适用于 api 路由,因此当您使用邮递员发送请求时,它将适用于您的情况。但是你不能对 web.php 中定义的路由做同样的事情。

Laravel 在他们的文档中为此提供了一个解决方案。您可以在 csrf 中间件中为此路由添加例外。

但是,您也可以通过将路由的 URI 添加到 VerifyCsrfToken 中间件的 $except 属性来排除路由:

https://laravel.com/docs/5.4/csrf#csrf- exclude-uris

您可以在以下位置找到此中间件:

app/Http/Middleware/VerifyCsrfToken.php

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'add/talent',
    ];
}

添加异常后,您应该可以使用邮递员发送请求!一般建议将此类路由放在 api.php 中,而不是 web.php 中。Csrf 验证是 Laravel 提供的一项安全功能,除非您有充分的理由这样做,否则您不应该排除它 :)

希望能帮助到你。如果您有任何疑问,请随时询问。


推荐阅读