Laravel 有 2 个同名字段保存到不同表中的注册表单
Laravel register form with 2 fields with same name saved to different tables
我有一个自定义用户注册表,它同时注册了一个新用户和一个新教会团队。所以这会将数据保存到 2 个不同的 table,用户 table 和团队 table。该表格包括用户 phone 号码和教会团队 phone 号码的输入字段。出现的问题是输入到第二个 phone 字段的 phone 数字覆盖第一个字段并保存到两个 table 中,而不是分别保存它们。
这是注册表中的 phone 输入字段,这是第一个,它接收用户 phone 号码。第二个字段,接收教会团队 phone 编号,除了语言是 @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;
我想将两个不同的唯一 phone 数字保存到它们各自的 table 中,此代码中需要发生什么才能获得该结果?
这应该有效:
<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;
我有一个自定义用户注册表,它同时注册了一个新用户和一个新教会团队。所以这会将数据保存到 2 个不同的 table,用户 table 和团队 table。该表格包括用户 phone 号码和教会团队 phone 号码的输入字段。出现的问题是输入到第二个 phone 字段的 phone 数字覆盖第一个字段并保存到两个 table 中,而不是分别保存它们。
这是注册表中的 phone 输入字段,这是第一个,它接收用户 phone 号码。第二个字段,接收教会团队 phone 编号,除了语言是 @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;
我想将两个不同的唯一 phone 数字保存到它们各自的 table 中,此代码中需要发生什么才能获得该结果?
这应该有效:
<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;