【发布时间】:2016-06-07 01:39:05
【问题描述】:
我正在使用 Laravel 并尝试向标准 Bootstrap 注册表单添加一个新的选择字段。提交表单后,我无法将选择字段中指定的值显示在我的数据库中。所有其他字段(例如,用户名和电子邮件)都可以正常工作。
下面是代码的一部分,为简洁起见,删除了一些字段。我所做的只是添加选择字段而不更改其他 Laravel 文件中的任何代码。数据库有两个表。一个叫做机构,它有两列:“名称”和“instid”(主键)。另一个叫做users,有好几列,其中一列是“instid”(外键)。这个外键字段是我要发布所选值的字段。
提交表单后,如何让选择字段中指定的值显示在我的数据库中?
@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
【问题讨论】:
-
I cannot get the value specified in the select field to show up in my database你能不能至少指定你运行哪个版本的 Laravel,显示你的控制器的相关部分(或路由,或者你处理这个请求的任何地方),并展示你如何拥有在你的控制器中调试了这个? -
当然! Laravel 5. 部分问题是我不知道我需要更改控制器。到目前为止,Laravel 已经自行设置了控制器。当我在数据库中添加列时,是否有一个工匠命令会自动更新控制器?在下面 Jilson T. 的建议的帮助下,我想出了代码。我将很快更新我的解决方案版本。
-
据我所知,我只知道与此有关的
php artisan make:函数(尝试php artisan help make)。祝你好运!
标签: php html twitter-bootstrap laravel