假设我们有两个表:students
和courses
,它们之间是一个多对多的关系,一个学生可以选择多门课程,一门课程也可以被多个学生选择。我们将创建一个中间表course_student
来管理这个关系。
首先,在数据库中创建students
表和courses
表,以及中间表course_student
。可以使用以下迁移命令来执行迁移:
php artisan make:migration create_students_table --create=students
php artisan make:migration create_courses_table --create=courses
php artisan make:migration create_course_student_table --create=course_student
然后,对应的迁移文件将如下所示:
create_students_table.php
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('name');
// 添加其他所需字段
$table->timestamps();
});
}
create_courses_table.php
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->id();
$table->string('name');
// 添加其他所需字段
$table->timestamps();
});
}
create_course_student_table.php
public function up()
{
Schema::create('course_student', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('course_id');
$table->unsignedBigInteger('student_id');
// 添加其他所需字段
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
$table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
$table->timestamps();
});
}
接下来,我们需要在对应的模型中定义多对多关系,并指定中间表的名称。
Student.php 模型
class Student extends Model
{
protected $fillable = ['name'];
public function courses()
{
return $this->belongsToMany(Course::class, 'course_student');
}
}
Course.php 模型
class Course extends Model
{
protected $fillable = ['name'];
public function students()
{
return $this->belongsToMany(Student::class, 'course_student');
}
}
现在,我们就可以在控制器中使用多对多关系了。下面是一个简单的示例:
CoursesController.php 控制器
use App\Models\Course;
class CoursesController extends Controller
{
public function show($id)
{
$course = Course::find($id);
$students = $course->students;
return view('courses.show', compact('course', 'students'));
}
}
-
定义模型:
你需要创建两个模型类:
Student
和Course
。这些模型类将继承自 Eloquent 的基类Model
。在模型类中,你需要定义与其对应的数据库表名(如果与类名不同)以及它们之间的关联关系。例如,在
Student
模型中定义了与Course
模型的多对多关系:class Student extends Model { public function courses() { return $this->belongsToMany(Course::class, 'course_student'); } }
在
Course
模型中定义了与Student
模型的多对多关系:class Course extends Model { public function students() { return $this->belongsToMany(Student::class, 'course_student'); } }
在这些关系定义中,
belongsToMany
方法接收两个参数:关联的模型类和中间表的表名。 -
操作关联关系:
一旦模型关联关系定义完毕,你就可以使用 Eloquent ORM 来操作多对多关系了。
比如,获取一个学生选择的所有课程:
$student = Student::find(1); $courses = $student->courses;
在上面的代码中,我们使用
find
方法找到 id 为 1 的学生,然后通过访问courses
属性可以获取该学生选择的所有课程。同样,你也可以从课程模型开始获取选择该课程的所有学生:
$course = Course::find(1); $students = $course->students;
-
新增关联关系:
如果你想要给一个学生添加一门新课程,或给一门课程添加一个新的学生,可以通过关联关系的
attach
方法来实现。例如,将一门课程添加给某个学生:
$student = Student::find(1); $student->courses()->attach(1); // 课程ID为1
或者将一个学生添加到某个课程中:
$course = Course::find(1); $course->students()->attach(1); // 学生ID为1
在
attach
方法中,你可以传递课程ID或学生ID,Eloquent 将根据中间表的定义自动处理关联记录的创建。 -
移除关联关系:
同样地,如果你想要移除一个学生与课程的关联关系,或者一个课程与学生的关联关系,可以使用
detach
方法。例如,移除一个学生与某门课程的关联关系:
$student = Student::find(1); $student->courses()->detach(1); // 课程ID为1
或者移除某门课程与一个学生的关联关系:
$course = Course::find(1); $course->students()->detach(1); // 学生ID为1
在
detach
方法中,你可以传递课程ID或学生ID。