简体   繁体   中英

Add columns to pivot table in Laravel

I have 4 Models:

  1. Student
  2. Tutor
  3. Course
  4. Admin

I am able to fetch all the students and tutors with their Ids. I want to the admin to create Courses and store them in pivot Tables:

  • course_student
  • course_tutor

Yet, the relationships are not too clear for me. I assume that I need

belongsToMany

between Course and Student . The same goes for Tutor and Course am i right?

What is also not clear is, how can I select many values on the HTML side and submit them to the server.

Eg:

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');
}

Here is the 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'
    ];
}

I might let the admin selects multiple tutors and students.

How can I accomplish this?

Many Thanks.

As i see, your common model is Course so firstable:

Add in your Course model the following:

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

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

Then, you need to create the pivot table for both classes ( Student and Tutor ):

Add this to your migrations file (for student and tutor respectively):

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');
});

Please see the name of fields i am using and be sure you are using the same.

To get the students and tutors of a course you can do it by

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

Hope this help!

By the way this is a very usefull Eloquent Relationships Cheat Sheet you can use to aproach this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM