我正在尝试使用下拉列表中的ID将记录插入数据库。然后我尝试将它传递给我的模型并保存记录,但是它表示hero_id为空,即使请求应该从下拉列表的值中获取hero_id。
我的观点:
var C = A+2*B
我的控制器:
<form method='POST' action='/profile/addPlay'>
<div class="form-group col-xs-12">
<label for="title">Title</label>
<input type="text" value="{{old('title') }}" name="title" class="form-control" aria-describedby="emailHelp" >
</div>
<div class="form-group col-xs-12">
<label for="video">Video</label>
<input type="text" value="{{old('video') }}" name="video" class="form-control" >
</div>
<div class="form-group col-xs-12">
<label for="hero_id">Hero</label>
<select name="hero_id" class="form-control" id="hero_id">
@foreach ($hero as $heroes)
<option value="{{$heroes->id}}">{{$heroes->name}}</option>
@endforeach
</select>
</div>
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-primary">
Add your play
</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
@if(count($errors))
@foreach ($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
@endif
我的模特:
public function insertPlayProfile (Request $request,Hero $hero){
$this->validate(
$request,[
'title' => 'required|min:3',
'video' => 'required'
]
);
$play = new Plays($request->all());
$hero->addPlayOnProfile($play, Auth::user()->id);
return back();
}
我还在我的Plays模型中填写了user_id文件。
我收到此错误:
SQLSTATE [23000]:完整性约束违规:19 NOT NULL约束失败:plays.hero_id(SQL:插入&#34;播放&#34;(&#34;标题&#34;,&#34;视频&# 34;,&#34; user_id&#34;,&#34; hero_id&#34;,&#34; updated_at&#34;,&#34; created_at&#34;)值(thghfghgf,ghgfdhfg,1,2017- 01-22 21:23:56,2017-01-22 21:23:56))
有什么建议吗?