【问题标题】:Add columns to pivot table in Laravel在 Laravel 中向数据透视表添加列
【发布时间】:2018-02-08 06:07:52
【问题描述】:

我有 4 个模型:

  1. 学生
  2. 导师
  3. 课程
  4. 管理员

我可以使用他们的 ID 获取所有学生和导师。 我想让管理员创建 课程 并将它们存储在数据透视表中:

  • course_student
  • course_tutor

然而,这些关系对我来说并不太清楚。我认为我需要

belongsToMany

课程学生之间。 TutorCourse 也是如此,对吗?

同样不清楚的是,如何在 HTML 端选择多个值并将它们提交给服务器。

例如:

public function store(AdminCreateNewCourseRequest $request)
{
    $this->authorize('create-course');

    $course = new Course;

    $course->name               = $request->name;
    $course->tutor_id           = $request->tutor_id;
    $course->student_id         = $request->student_id;
    $course->spoken_language    = $request->spoken_language;
    $course->description        = $request->description;

    $course->save();

    What to do?

    return redirect($course->path())
        ->with('flash', 'The course has been published');
}

这里是 AdminCreateNewCourseRequest

public function rules()
{
    return [
        'name'          =>  'required|unique:courses|max:60',
        'tutor_id'      =>  [
            'required',
                            Rule::exists('tutors', 'id');
        ],
        'student_id'      =>  [
            'required',
            Rule::exists('students', 'id');
        ],
        'spoken_language'    =>  'required',
        'description'   =>  'required|max:255'
    ];
}

我可能会让管理员选择多个导师和学生。

我怎样才能做到这一点?

非常感谢。

【问题讨论】:

    标签: php html mysql database laravel


    【解决方案1】:

    如我所见,您的常用模型是 Course so firstable:

    在您的课程模型中添加以下内容:

    public function students()
        {
           return $this->belongsToMany(Student::class);
        }
    
    public function tutors()
        {
           return $this->belongsToMany(Tutor::class);
        }
    

    然后,您需要为两个班级(学生和导师)创建数据透视表:

    将此添加到您的迁移文件(分别用于学生和导师):

    Schema::create('course_students', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('course_id')->unsigned()->index();
        $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
        $table->integer('student_id')->unsigned()->index();
        $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
    });
    
    Schema::create('course_tutors', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('course_id')->unsigned()->index();
        $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
        $table->integer('tutor_id')->unsigned()->index();
        $table->foreign('tutor_id')->references('id')->on('tutors')->onDelete('cascade');
    });
    

    请查看我正在使用的字段的名称,并确保您使用的是相同的。

    要获得课程的学生和导师,您可以通过

    $course = App\Course::find($id);
    $students = $course->students;
    $tutors = $course->tutors;
    

    希望对您有所帮助!

    顺便说一句,这是一个非常有用的Eloquent Relationships Cheat Sheet,你可以用它来解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 2016-04-29
      • 2019-06-12
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 2016-01-23
      相关资源
      最近更新 更多