嗨,我想将一种形式的数据存储到2个db表中,但无法执行:在我的代码中,我希望老师可以添加多个学生,将老师数据保存到教师表中,并将学生数据保存到学生表中我的数据库表的教师ID屏幕截图为:https://ibb.co/ctr7FDP https://ibb.co/nwmcnPX,这是视图https://ibb.co/CBytRCR,根据此视图,我想在学生表中插入多个具有其教师ID的学生姓名, ,任何准则和Note ,,, m使用资源路由
教师控制代码:
at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.write(JavaDefaultSerializers.java:107)
at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.write(StreamSerializerAdapter.java:43)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toBytes(AbstractSerializationService.java:154)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toBytes(AbstractSerializationService.java:129)
at com.hazelcast.spi.impl.operationservice.impl.OutboundResponseHandler.toNormalResponsePacket(OutboundResponseHandler.java:156)
at com.hazelcast.spi.impl.operationservice.impl.OutboundResponseHandler.sendNormalResponse(OutboundResponseHandler.java:128)
at com.hazelcast.spi.impl.operationservice.impl.OutboundResponseHandler.sendResponse(OutboundResponseHandler.java:103)
at com.hazelcast.spi.Operation.sendResponse(Operation.java:416)
at com.hazelcast.executor.impl.DistributedExecutorService$CallableProcessor.sendResponse(DistributedExecutorService.java:314)
at com.hazelcast.executor.impl.DistributedExecutorService$CallableProcessor.run(DistributedExecutorService.java:297)
at com.hazelcast.util.executor.CachedExecutorServiceDelegate$Worker.run(CachedExecutorServiceDelegate.java:227)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at com.hazelcast.util.executor.HazelcastManagedThread.executeRun(HazelcastManagedThread.java:64)
at com.hazelcast.util.executor.HazelcastManagedThread.run(HazelcastManagedThread.java:80)
刀片文件:
public function store(Request $request)
{
$request->validate([
'teacher_name' => 'required',
]);
$teacher = new Teacher;
$teacher->teacher_name = $request->teacher_name;
$teacher->save();
return redirect('teacher');
}
老师模型:
<form method="POST" action="{{ route('teacher.store') }}">
@csrf
<div class="form-group">
<label for="">Teacher Name</label>
<input type="text" name="teacher_name" class="form-control input {{ $errors->has('teacher_name') ? 'is-danger' : ''}}" id="" placeholder="Teacher Name">
</div>
<div class="form-group">
<label class="text-right">Enter Student Name</label>
<div class="col-md-8">
<div class="wrapper">
<div><input type="text" name="student_name[]" class="form-control input-lg" placeholder="Enter Student Name" /></div>
</div>
<p><button class="add_fields">Add More Students</button></p>
</div>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="submit">
@include('errors')
</form>
学生模型:
public function student()
{
$this->hasMany('App\Student');
}
答案 0 :(得分:1)
public function students() //student to students
{
$this->hasMany('App\Student');
}
插入代码应为
$teacher->students()->createMany([[$student1],[$student2]]);
答案 1 :(得分:0)
假设您要从一个名为student_id
的变量中以表格的形式引入多个学生,则可以在保存attach()
之后使用Laravel的teacher
方法附加他们。 。
$teacher->student()->attach($request->get('student_id'));
由于您有很多学生要教,我建议您将 Teacher 模态上的方法重命名为复数(students
)。
但是,如果您的FK是我的话,我建议您查看表单student_id[]
而不是名字-让name
来自学生领域,但为了要进行连接,您需要阵列中的ids
,而不是name
。