我有一个自定义用户注册表,它注册一个新用户和一个新的教会团队在同一时间。因此,这会将数据保存到两个不同的表中,即用户表和团队表。此表单包括一个输入字段,用于输入用户的电话号码以及教会团队的电话号码。出现的问题是,在第二个电话字段中输入的电话号码将覆盖第一个电话号码并保存到两个表中,而不是分别保存它们。
这是注册表中的电话输入字段,这是第一个字段,它接收用户的电话号码。第二个字段接收教堂团队的电话号码,除了语言是@lang('global.teams.fields.phone')之外,其他字段都是相同的。
<div class="form-group{{ $errors->has('phone') ? ' has-error' : '' }}">
<label for="phone" class="col-md-4 control-label">@lang('global.app_phone')</label>
<div class="col-md-6">
<input id="phone" type="text" class="form-control" name="phone" value="{{ old('phone') }}" >
@if ($errors->has('phone'))
<span class="help-block">
<strong>{{ $errors->first('phone') }}</strong>
</span>
@endif
</div>
</div>
下面是我的控制器验证器和创建代码:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:191',
'email' => 'required|email|max:191|unique:users',
'password' => 'required|min:6|confirmed',
'church_name' => 'required|max:191',
'address' => 'required|max:191',
'state' => 'required|max:191',
'city' => 'required|max:191',
'zip' => 'required|max:191',
'phone' => 'required|max:191',
'g-recaptcha-response' => [new ReCaptcha],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
$team = Team::create([
'name' => $data['church_name'],
'address' => $data['address'],
'city' => $data['city'],
'state' => $data['state'],
'zip' => $data['zip'],
'phone' => $data['phone'],
]);
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => bcrypt($data['password']),
'team_id' => $team->id
]);
$user->role()->attach(config('app_service.default_role_id'));
return $user;
我想要将两个不同的唯一电话号码保存到各自的表中,在此代码中需要发生什么才能得到结果?
发布于 2019-06-27 07:02:51
这应该是可行的:
<div class="form-group{{ $errors->has('user_phone') ? ' has-error' : '' }}">
<label for="user_phone" class="col-md-4 control-label">@lang('global.app_user_phone')</label>
<div class="col-md-6">
<input id="user_phone" type="text" class="form-control" name="user_phone" value="{{ old('user_phone') }}" >
@if ($errors->has('user_phone'))
<span class="help-block">
<strong>{{ $errors->first('user_phone') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('church_team_phone') ? ' has-error' : '' }}">
<label for="church_team_phone" class="col-md-4 control-label">@lang('global.app_church_team_phone')</label>
<div class="col-md-6">
<input id="church_team_phone" type="text" class="form-control" name="church_team_phone" value="{{ old('church_team_phone') }}" >
@if ($errors->has('church_team_phone'))
<span class="help-block">
<strong>{{ $errors->first('church_team_phone') }}</strong>
</span>
@endif
</div>
</div>
和控制器:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:191',
'email' => 'required|email|max:191|unique:users',
'password' => 'required|min:6|confirmed',
'church_name' => 'required|max:191',
'address' => 'required|max:191',
'state' => 'required|max:191',
'city' => 'required|max:191',
'zip' => 'required|max:191',
'user_phone' => 'required|max:191',
'church_team_phone' => 'required|max:191',
'g-recaptcha-response' => [new ReCaptcha],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
$team = Team::create([
'name' => $data['church_name'],
'address' => $data['address'],
'city' => $data['city'],
'state' => $data['state'],
'zip' => $data['zip'],
'phone' => $data['church_team_phone'],
]);
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['user_phone'],
'password' => bcrypt($data['password']),
'team_id' => $team->id
]);
$user->role()->attach(config('app_service.default_role_id'));
return $user;
https://stackoverflow.com/questions/56781632
复制相似问题