在我的用户注册页面上,我有一个表格,他们填写了他们的姓名,电子邮件等。我将把成就添加到他们需要填写的内容列表中。但是,我希望用户能够投入尽可能多的成就。稍后我将在他们的个人资料页面上显示这些成就。
这是我的注册表,如果有帮助的话。
<form class="form-horizontal" role="form" method="POST" action="/register/make" enctype="multipart/form-data">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">First name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="first_name" value="{{ old('first_name') }}">
@if ($errors->has('first_name'))
<span class="help-block">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('family_name') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Family Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="family_name" value="{{ old('family_name') }}">
@if ($errors->has('family_name'))
<span class="help-block">
<strong>{{ $errors->first('family_name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password" value="{{ old('password') }}">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation" value="{{ old('password') }}">
@if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('year') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Year Graduated</label>
<div class="col-md-6">
<input type="text" class="form-control" name="year" value="{{ old('year') }}">
@if ($errors->has('year'))
<span class="help-block">
<strong>{{ $errors->first('year') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('mark') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Final Mark</label>
<div class="col-md-6">
<input type="text" class="form-control" name="mark" value="{{ old('mark') }}">
@if ($errors->has('mark'))
<span class="help-block">
<strong>{{ $errors->first('mark') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Description</label>
<div class="col-md-6">
<textarea rows="10" class="form-control" name="description" value="{{ old('description') }}">@if ($errors->has('description'))
<span class="help-block">
<strong>{{ $errors->first('description') }}</strong>
</span>@endif</textarea>
</div>
</div>
<div class="form-group{{ $errors->has('picture') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Upload a photo of yourself</label>
<div class="col-md-6">
<input type="file" class="form-control" name="picture">
@if ($errors->has('picture'))
<span class="help-block">
<strong>{{ $errors->first('picture') }}</strong>
</span>
@endif
<p>We recommend 300x300 pixels for best results.</p>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<p>I agree to the <a href="/terms">terms and conditions</a></p>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button name="register" type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i>Register
</button>
</div>
</div>
@if (count($errors))
<ul>
@foreach($errors->all() as $error)
<li class="text-danger"> {{$error}} </li>
@endforeach
</ul>
@endif
</form>
只需在字段名称中使用括号,如下所示:
<input type="text" name="achievements[]"/>
然后使用 javascript/jquery,您可以通过为用户提供添加新成就的按钮以编程方式创建更多字段。
您可以探索的另一个选项是使用 select2 等库。它具有标记功能。
在后端,PHP 会将值转换为数组。然后 Laravel 让你从请求访问这个数组,如下所示:
public function store(Request $request)
{
$achievements = $request->get('achievements');
}
从那里,您可以像往常一样迭代并保存每个成就。