【发布时间】:2020-01-04 23:29:19
【问题描述】:
我已经创建了学生和学科模型及其 CRUD 如何为学生注册一个 subject_id 集合
我已经使用 subject_id 和 student_id 创建了一个新模型“注册”,但我不知道如何向学生添加一个 subject_id 的集合
这是我的学生模型
class Student extends Model
{
protected $table = 'students';
protected $fillable = [
'firstname','middlename','lastname','index_no','parent_id',
'regular_or_weekend',
'course_id',
'nationality',
'image'
]
public function subjects()
{
return $this->hasMany(Subject::class);
}
}
这是我的学科课
class Subject extends Model
{
protected $table = 'subjects';
protected $fillable = ['subject_code','subject_name','credit_hours'];
protected $cast =[
'credit_hours' => 'Integer',
];
public function student()
{
return $this->belongsTo(Student::class);
}
}
【问题讨论】:
-
看看文档,这是你需要的:laravel.com/docs/master/…
-
@nakov 是正确的——Laravel 使用约定来命名关系——你需要将它们添加到你的数据库和
fillable。 (或覆盖)。但文档会解释。 -
这个假设集合中的主题已经保存在数据库中还是都是新的对象?顺便说一句,你没有给我任何反馈,接受或赞成我对你关于正则表达式的问题的回答,所以我决定不再回答你的问题。再见@eons。
-
对不起@dparoli。它永远不会再发生了。