Reputation: 91
I'm working with a CRUD controller, trying to create a person. A person belongs to a country, I have this relationship set up correctly.
In my create view, I have text boxes for user input, and a dynamic dropdown menu created from querying the Country table.
My error is when I go to create a person, I get an error - General error: 1364 Field 'country_id' doesn't have a default value.
I know this is because I'm not inserting anything into country_id because I simply don't know how to get the ID from the Country dynamic dropdown.
In my create.blade.php I have :
{{csrf_field()}}
{{method_field('POST')}}
<div>
<input type="text" name="name" placeholder="Name"> <br>
<input type="text" name="age" placeholder="Age"> <br>
<input type="text" name="role" placeholder="Role"> <br>
<input type="text" name="batting" placeholder="Batting"> <br>
<input type="text" name="bowling" placeholder="Bowling"> <br>
<input type="text" name="odiRuns" placeholder="Odi Runs"> <br>
<input type="text" name="image" placeholder="Image name"> <br>
<label>Select a country:</label>
<select name="country" class="form-control" style="width:350px">
<option value="">--- Select Country ---</option>
@foreach ($countries as $country => $value)
<option value="{{ $country }}">{{ $value->name }}</option>
@endforeach
</select>
</div>
<button type="submit" class="button">Create a player</button>
</div>
</form>
In my controller I have -
$players = new Player();
$players->name = request('name');
$players->age = request('age');
$players->role = request('role');
$players->batting = request('batting');
$players->bowling = request('bowling');
$players->odiRuns = request('odiRuns');
$players->image = request('image');
$players->country_id = request('???');
$players->save();
Does anyone know how I can get the country_id value from my dynamic dropdown? Thank you
Upvotes: 0
Views: 631
Reputation: 670
Shouldn't it be $players->country_id = request('country');
as the select option name is country
Upvotes: 1