php - 如何使用单一形式在两个具有关系的表中添加值?
问题描述
我是 Laravel 的新手,我想使用单一表单在两个表中添加数据。我的桌子和他们的关系是
我想为用户添加所有字段,然后选择class_id,然后通过下拉菜单选择section_id。
然后注册,我希望在注册数据之后。注册表应该具有该新用户注册和选择的类和部分 id 的 user_id。
学生表的字段
<form method="POST" action="{{url('/storeStudent')}}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>
@if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>
@if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<input id="role_id" type="hidden" name="role_id" value="2">
以注册表开头的字段
<select class="" name="class_id">
<option value="null">Class</option>
@foreach($cid as $cc)
@if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{{$counterr++}}
@else
<option value="{{$cc->id}}">{{$cc->title}}</option>
@endif
@endforeach
</select>
<select section="" name="section_id">
<option value="null">Section</option>
@foreach($sec as $sc)
@if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
@else
<option value="{{$sc->id}}">{{$sc->title}}</option>
@endif
@endforeach
</select>
</div>
</div>
</form>
注册控制器
protected function create(array $data)
{
return Registration::create([
'reg_id' => $data['reg_id'],
'user_id' => $data['user_id'],
'class_id' => $data['class_id'],
'section_id' => $data['section_id'],
]);
}
public function store(Request $request)
{
registration::create($request->all());
return back();
}
商店学生
public function storeStudent(Request $request)
{
$user=new User();
$user->name=$request->name;
$user->password=Hash::make($request->password);
$user->email=$request->email;
$user->role_id=2;
$user->parent_id=$request->parent_id;
$user->save();
return back();
}
现在我希望将要注册的相同 user_id 也应该保存在注册表中
我在控制器中使用了简单的存储方法来保存数据。
解决方案
这很容易..您应该创建用户并使用它的 id...像这样:
学生控制器:
public function storeStudent(Request $request)
{
$created_user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
'parent_id' => $request->parent_id,
'role_id' => $request->role_id,
]);
//And after that you need to store user_id in Registration table , so:
Registration::create([
'user_id' => $created_user->id,
'class_id' => $request->class_id,
'section_id' => $request->section_id,
]);
return redirect()->back();
}
希望能帮助到你
推荐阅读
- reactjs - Gatsby 将目录与页面滚动同步并设置活动链接的样式
- ios - 将 Quicktime (.mov) 从 iPhone 转换为 mp4
- c# - Xamarin.Forms。安卓。无法隐藏键盘
- docker - docker nginx-proxy:如何从一个容器“卷曲”到另一个容器?
- python-3.x - 使用 Prophet 的 Python 时间序列
- java - QueryDsl 投影 ElementCollection
- php - 当您在具有命名空间的类的文件名中添加 .class.php 时是否存在问题?
- git - git mv 一个 bash 脚本,用于重命名特定分支的文件夹,更改所有分支
- wso2 - wso2am-analytics-2.6.0的网页中文显示不正常
- nlog - NLog 日志不记录