【发布时间】:2020-04-14 18:25:30
【问题描述】:
我想创建一个有一些课程的学生。
我创建了两个不同的表:一个学生表
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('student_name');
$table->string('first_name');
$table->string('last_name');
$table->string('email');
});
}
还有一个课程表。
public function up()
{
Schema::create('student_courses', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('student_id');
$table->string('course_name');
$table->timestamps();
});
}
students表创建学生的信息,并保存在student表中。
我想用学生 ID 将课程保存到课程表中。他们应该有一对多的关系。如何使用特定学生的 id 将不同的课程保存到课程表中?
【问题讨论】:
标签: php mysql laravel eloquent