[英]Form data is not submitting into database using laravel 5.4 version
我試圖簡單地添加我的表單數據,但是我不知道我在哪里錯了我的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();
}
}
和我的模型:
天賦類擴展模型{protected $ 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郵遞員下測試我的路由並點擊我的帖子,請求它使用URL成功將數據添加到數據庫中: http : //127.0.0.1 :8000/api/add/talent
但是,如果我刪除了api並只是使用url http://127.0.0.1:8000/add/talent命中了請求,它會在VerifyCsrfToken.php第68行中說TokenMismatchException我不知道我在哪里做錯了為什么我的表單未提交我的數據
任何幫助將不勝感激!
默認情況下,Laravel在網絡路由中使用csrf中間件來檢查和驗證請求是否具有有效的csrf令牌。 這不適用於api路由,因此當您與郵遞員發送請求時,此方法將適用於您的情況。 但是您不能對web.php中定義的路由執行相同的操作。
Laravel在其文檔中為此提供了解決方案。 您可以在csrf中間件中為此路由添加一個例外。
但是,您也可以通過將其URI添加到VerifyCsrfToken中間件的$ except屬性中來排除路由:
https://laravel.com/docs/5.4/csrf#csrf-clusion-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提供的一項安全功能,除非您有充分的理由,否則不應該將其排除在外:)
希望能幫助到你。 隨時詢問您是否有任何疑問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.