【发布时间】:2020-12-23 08:56:52
【问题描述】:
我想获取特定笔记的课程。这里注意 id 和 class id 在数据透视表中。
Note.php
public function classes()
{
return $this->belongsToMany(MyClass::class);
}
MyClass.php
public function notes()
{
return $this->belongsToMany(Note::class);
}
我正在成功保存数据
$note->classes()->attach($request->class);
MyClass 迁移
Schema::create('my_classes', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
$table->boolean('isDeleted')->default(false);
$table->dateTime('deletedAt')->nullable();
});
笔记迁移
Schema::create('notes', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('description')->nullable();
$table->string('image')->nullable();
$table->timestamps();
$table->boolean('isDeleted')->default(false);
$table->dateTime('deletedAt')->nullable();
});
my_class_note 迁移
Schema::create('my_class_note', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('my_class_id')->constrained();
$table->foreignId('note_id')->constrained();
$table->timestamps();
});
在获取特定笔记的课程方面需要帮助。一个笔记可以有多个类。
【问题讨论】:
标签: laravel eloquent relationship