I am trying to insert a record into the database using an id from a dropdown. I am then trying to pass it to my model and save the record, however it says that the hero_id is null, even though the request should pick up the hero_id from the value of the dropdown.
My view:
<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
My controller:
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();
}
My model:
public function addPlayOnProfile(Plays $play, $userId){
$play->user_id = $userId;
return $this->plays()->save($play);
}
I have also made the user_id fillable in my Plays model.
I am getting this error:
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: plays.hero_id (SQL: insert into "plays" ("title", "video", "user_id", "hero_id", "updated_at", "created_at") values (thghfghgf, ghgfdhfg, 1, , 2017-01-22 21:23:56, 2017-01-22 21:23:56))
Any suggestions?