如何在laravel中向透视表添加信息?

xt0899hw  于 2023-01-18  发布在  其他
关注(0)|答案(2)|浏览(102)

学生和课程之间有很多很多的关系。
下面是我的模型和透视表:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'surname',
        'age',
        'tdah',
        'description',
        'hobbies',
    ];

   /*  public function course(){
        return $this->belongsTo(Course::class);
    } */

    public function data(){
        return $this->hasMany(Datum::class, 'student_id', 'id');
    }

    public function configurations(){
        return $this->hasMany(Configuration::class, 'student_id', 'id');
    }

    public function courses(){
        return $this->belongsToMany(Course::class, 'course_student', 'student_id', 'course_id');
    }

}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'academic_course_id',
        'user_id',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function academicCourse()
    {
        return $this->belongsTo(AcademicCourse::class);
    }

    public function planification(){
        return $this->hasOne(Planification::class, 'course_id', 'id');
    }

    public function subjects(){
        return $this->hasMany(Subject::class, 'course_id', 'id');
    }

    /* public function students(){
        return $this->hasMany(Student::class, 'course_id', 'id');
    } */

    public function students(){
        return $this->belongsToMany(Student::class, 'course_student', 'course_id', 'student_id');
    }
}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('course_student', function (Blueprint $table) {
            $table->id();

            $table->foreignId('course_id')->constrained()->onDelete('cascade');
            $table->foreignId('student_id')->constrained()->onDelete('cascade');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('course_student');
    }
};

我想创建一个属于某个组的学生。我知道如何创建学生和组,但我想在course_student透视表中为该学生创建一个注册表。我的StudentController方法存储为:

public function store(Request $request)
    {
        Student::create($request->all());
        $courses = Course::all();
        $academicCourses = AcademicCourse::all();
        return view('course.index', compact('courses', 'academicCourses')); 
    }

它创建了一个新的学生,但我不知道如何为透视表创建一个新的注册表。
我想做的事情是这样的:

course_student::create(request)

在我的存储方法中,但我认为不是那样。

ndh0cuux

ndh0cuux1#

$student->courses()->attach($courseId);

https://laravel.com/docs/9.x/eloquent-relationships#updating-many-to-many-relationships

e4yzc0pl

e4yzc0pl2#

在Laravel中,您可以使用attach函数来完成此操作,Attach()函数有两个参数:
1-在您情况下的另一个外键是attach(course_id)
2-(可选)数据透视表中任何其他列的数组,例如:

attach(course_id,['users_id'=>$usersId])

在本例中,您将通过student模型中的对象在透视表(course_student)中创建记录。

$student->courses()->attach($course_id);

注意:还有另一个方法可以做相反的事情**detach()**但是你需要小心使用它如果你不带任何参数的话它会删除(course_student)里面所有属于student对象的记录所以指定你想要删除的课程:

$student->courses()->detach($course_id);

希望这能帮到你...

相关问题