這是我的單選按鈕的代碼如何驗證laravel 5.4單選按鈕?
<div class="radio">
<label><input type="radio" name="gander" value="male">male </label>
<label><input type="radio" name="gander" value="female">female</label>
</div>
和我的控制器代碼是
//validate this form
$this->validate(request(),[
'title' => 'required',
'body' => 'required',
'gander'=> 'in:male,female'
]);
$post = new Post;
$post->title = $request['title'];
$post->body = $request['body'];
$post->status= $request['status'];
$post->male = $request['male'];
$post->female= $request['female'];
$post->save();
//And then redirect to the home
return redirect('blog');
}
當我提交這份表格顯示此錯誤 SQLSTATE [23000]:完整性約束違規:1048列「男「不能爲空(SQL:插入posts
(title
,body
,status
,male
,female
,updated_at
,created_at
)值(SDF,sdfsfsd,0,,,2017年3月1日13時09分54秒, 2017-03-01 13:09:54))
這意味着兩個單選按鈕數據都需要數據庫字段。然後進入我的表有這些領域的「標題」,「體」,「地位」,「男」,'女「created_at」 &「的updated_at」
我的問題是如何驗證單選按鈕&如何插入數據用單選按鈕值?謝謝。
爲什麼你有'男性'和'女性'兩個差異字段?你能詳細說明嗎?你可以簡單地選擇一個字段「性別」。並填寫它是男性還是女性的價值。你的方式是錯誤的!爲了表明我的錯誤,tnx prakash大號 –
。現在我通過tinyinteger數據類型創建我的模式字段。現在它的作品。 – Masum
太棒了!祝你好運 –