Reputation: 185
I am using Laravel and trying to add a new select field to the standard Bootstrap registration form. I cannot get the value specified in the select field to show up in my database after submitting the form. All of the other fields (e.g., user name and email) are working fine.
Below is a portion of the code with some fields removed for brevity. All I've done is add the select field without altering any code in other Laravel files. The DB has two tables. One is called institutions, which has two columns: "name" and "instid" (primary key). The other is called users, which has several columns, one of which is "instid" (foreign key). This foreign key field is the one to which I would like to post the selected value.
How do I get the value specified in the select field to show up in my database after submitting the form?
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<!-- use for="name" so that clicking on label places cursor in input field with id="name" -->
<label class="col-md-4 control-label" for="name">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}">
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<div class="form-group">
<label class="col-md-4 control-label" for="instid">Institution</label>
<div class="col-md-6">
<select class="form-control" id="instid" name="instid" required="required">
<option value="" selected disabled>Choose an institution.</option>
<?php
$institutions = App\Institution::all();
foreach($institutions as $institution){
echo "<option value=\"" . $institution->instid . "\">" . $institution->name . "</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i>Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Upvotes: 0
Views: 1543
Reputation: 185
Jilson T.'s answer helped me to the following solution:
I left the code in my original question unaltered. (Jilson's suggested changes to the select object are a more eloquent use of Laravel's features but do not change the functionality.)
In the Laravel Http directory, User.php content
protected $fillable = [
'name', 'email', 'password',
];
should be:
protected $fillable = [
'name', 'email', 'instid', 'password',
];
In the auth controllers, AuthController.php content
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
should be:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'instid' => 'required|integer',
'password' => 'required|confirmed|min:6',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'instid' => $data['instid'],
'password' => bcrypt($data['password']),
]);
}
Upvotes: 0
Reputation: 7303
<select class="form-control" id="instid" name="instid" required="required">
<option value="" selected disabled>Choose an institution.</option>
@foreach($institutions as $institute)
<option value="{{ institute->id }}">$institution->name</option>
@endforeach
</select>
Now in your controller: $request->input('instid');
public function store(Request $request)
{
$instituteId = $request->input('instid');
}
Upvotes: 1