简体   繁体   English

表单数据未使用laravel 5.4版本提交到数据库

[英]Form data is not submitting into database using laravel 5.4 version

I am trying to simply adding my form data but i dont know where i am wrong my html view: 我试图简单地添加我的表单数据,但是我不知道我在哪里错了我的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>

My controller: 我的控制器:

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();
    }
}

and my model: 和我的模型:

class Talent extends Model { protected $table = 'add_talent_1'; 天赋类扩展模型{protected $ table ='add_talent_1';

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

} and my routes/web.php }和我的路线/web.php

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

if i test my route under routes/api.php usig postman and hit my post request it successfully add data into database using url: http://127.0.0.1:8000/api/add/talent 如果我在routes / api.php usig邮递员下测试我的路由并点击我的帖子,请求它使用URL成功将数据添加到数据库中: http : //127.0.0.1 :8000/api/add/talent

but if i removed api and simply hit request using url http://127.0.0.1:8000/add/talent it says TokenMismatchException in VerifyCsrfToken.php line 68 i dont know where i am doing my wrong why my form is not submitting my data 但是,如果我删除了api并只是使用url http://127.0.0.1:8000/add/talent命中了请求,它会在VerifyCsrfToken.php第68行中说TokenMismatchException我不知道我在哪里做错了为什么我的表单未提交我的数据

Any help will be highly appreciated! 任何帮助将不胜感激!

By default Laravel uses the csrf middleware in web routes to check and verify whether the request has a valid csrf token. 默认情况下,Laravel在网络路由中使用csrf中间件来检查和验证请求是否具有有效的csrf令牌。 This is not applicable in api routes, so it will work in your case when you send the request with postman. 这不适用于api路由,因此当您与邮递员发送请求时,此方法将适用于您的情况。 But you cannot do the same for a route defined in web.php. 但是您不能对web.php中定义的路由执行相同的操作。

Laravel offers a solution for this in their documentation though. Laravel在其文档中为此提供了解决方案。 You can add an exception for this route in csrf middleware. 您可以在csrf中间件中为此路由添加一个例外。

However, you may also exclude the routes by adding their URIs to the $except property of the VerifyCsrfToken middleware: 但是,您也可以通过将其URI添加到VerifyCsrfToken中间件的$ except属性中来排除路由:

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

You can find this middleware in: 您可以在以下位置找到该中间件:

app/Http/Middleware/VerifyCsrfToken.php 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',
    ];
}

Once you add the exception, you should be able to send the request using postman! 添加例外后,您应该可以使用邮递员发送请求! Generally, it's recommended to put such routes in api.php, not in web.php. 通常,建议将此类路由放在api.php中,而不是在web.php中。 Csrf validation is a security feature given by Laravel and you shouldn't be excluding it unless you have a good reason to do so :) Csrf验证是Laravel提供的一项安全功能,除非您有充分的理由,否则不应该将其排除在外:)

Hope it helps. 希望能帮助到你。 Feel free to ask if you have any doubts. 随时询问您是否有任何疑问。

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

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