我有一些Laravel模型,它们是通过belongsToMany关系中的数据透视表关联的。
现在,我尝试在Laravel Lighthouse中创建一个变异,以加入模型并填充枢轴值。
我不知何故不知道该怎么做。
课程模型如下所示:
class Course extends Model
{
public function programs(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Program::class, 'Course_Program')
->withPivot('id', 'year_id')
->withTimestamps();
}
}
我的GraphQL代码如下所示:
type Mutation {
createCourse(input: CreateCourseInput! @spread): Course! @create
}
type Course {
id: ID!
name: String!
programs: [ProgramWithPivot!]! @belongsToMany
}
type Program {
id: ID!
name: String!
}
type ProgramWithPivot {
id: ID!
name: String!
year_id: ID!
}
input CreateCourseInput {
name: String!
programs: CreateCourseProgramsRelation!
}
input CreateCourseProgramsRelation {
create: [CreateCourseProgramInput!]
}
input CreateCourseProgramInput {
id: ID!
year_id: ID!
}
问题是,当我尝试在课程中创建程序时,如下所示:
mutation {
createCourse(input: {
name: "new cours"
programs: {
create: [{
id: 1
year_id: 2
}]
}
}) {
id
programs {
id
year_id
}
}
}
Laravel Lighthouse尝试将数据插入Program表中(并抱怨Program.name没有默认值)。
但是,我想将数据(course_id,year_id和program_id)插入Course_Program表中。
如何告诉Laravel Lighthouse将数据插入数据透视表?
非常感谢您从事公关工作!病情跟进并阅读讨论。
我发现您的PR已在Lighthouse版本4.8中被接受。如果其他人有疑问,可以在这里找到文档:lighthouse-php.com/4.8/eloquent/…