dsce23640 2018-07-07 17:05
浏览 57

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

I am trying to simply adding my form data but i dont know where i am wrong my html view:

<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 $fillable = ['first_name','middle_name','last_name','email_address','profile_summary'];

} and my routes/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

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

Any help will be highly appreciated!

  • 写回答

1条回答 默认 最新

  • duanliexi1052 2018-07-07 18:06
    关注

    By default Laravel uses the csrf middleware in web routes to check and verify whether the request has a valid csrf token. This is not applicable in api routes, so it will work in your case when you send the request with postman. But you cannot do the same for a route defined in web.php.

    Laravel offers a solution for this in their documentation though. You can add an exception for this route in csrf middleware.

    However, you may also exclude the routes by adding their URIs to the $except property of the VerifyCsrfToken middleware:

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

    You can find this middleware in:

    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. 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 :)

    Hope it helps. Feel free to ask if you have any doubts.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端