-
-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to hiding "pivot" attribute from belongsToMany relationships? #366
Comments
Can you share the code you are running? |
I have model with code below:
'use strict'
const Model = use('Model')
class Class extends Model {
/**
* Filter by class.
*
* @param {*} query
* @param {*} type
*/
static scopeByClass (query, class_id) {
if (class_id) {
if (class_id instanceof Array) {
query.whereIn('id', class_id)
} else {
query.where('id', class_id)
}
}
}
/**
* Filter by course.
*
* @param {*} query
* @param {*} type
*/
static scopeByCourse (query, course_id) {
if (course_id) {
query.whereHas('courses', query => {
if (course_id instanceof Array) {
query.whereIn('course_id', course_id)
} else {
query.where('course_id', course_id)
}
})
}
}
/**
* Filter by teacher.
*
* @param {*} query
* @param {*} type
*/
static scopeByTeacher (query, teacher_id) {
if (teacher_id) {
query.whereHas('teachers', query => {
if (teacher_id instanceof Array) {
query.whereIn('user_id', teacher_id)
} else {
query.where('user_id', teacher_id)
}
})
}
}
/**
* Teachers.
*
* @return Array<User>
*/
teachers () {
// return this.hasMany('App/Models/ClassTeacher')
return this.belongsToMany('App/Models/User').pivotModel(
'App/Models/ClassTeacher'
)
}
/**
* Courses.
*
* @return Array<Course>
*/
courses () {
return this.belongsToMany('App/Models/Course').pivotModel(
'App/Models/ClassCourse'
)
}
/**
* Students
*
* @return Array<User>
*/
students () {
return this.belongsToMany('App/Models/User').pivotModel(
'App/Models/UserClass'
)
}
}
module.exports = Class On my method async index ({ request, response, view }) {
const {
page,
class: class_id,
course: course_id,
teacher: teacher_id
} = request.get()
const classes = await Class.query()
.orderBy('created_at', 'DESC')
.with('teachers')
.with('courses')
.with('students')
.withCount('teachers as total_teachers')
.withCount('courses as total_courses')
.withCount('students as total_students')
.byClass(class_id)
.byCourse(course_id)
.byTeacher(teacher_id)
.paginate(page)
return classes
} And object received in response of request: {
"total": 2,
"perPage": 20,
"page": 1,
"lastPage": 1,
"data": [
{
"id": 15,
"title": "The title",
"description": "The class description",
"created_at": "2018-08-09 01:47:22",
"updated_at": "2018-08-09 01:47:22",
"teachers": [
{
"id": 8,
"username": "username",
"first_name": "first_name",
"last_name": "last_name",
"pivot": {
"user_id": 8,
"class_id": 15
}
}
],
"courses": [
{
"id": 2,
"title": "Title",
"description": "The course description",
"created_at": "2018-08-09 00:52:40",
"updated_at": "2018-08-09 00:52:40",
"pivot": {
"course_id": 2,
"class_id": 15
}
}
],
"students": [
{
"id": 9,
"username": "username",
"first_name": "first_name",
"last_name": "last_name",
"pivot": {
"user_id": 9,
"class_id": 15
}
}
],
"__meta__": {
"total_teachers": 1,
"total_courses": 1,
"total_students": 1
}
}
]
} I would like to hide the "pivot" object within the relationships in the objects "teachers", "students", "courses"... If you have any questions, let me know! Thank you. |
Ahh okay. I don't think it's possible right now. Within the pivot model you can set hidden fields I will add this to the enhancements and will try to work on it later |
Just dropping by to show my support for this Enhancement-request. |
The newer release of AdonisJs Lucid Let's imagine, you want to include the class Teacher extends Model {
get pivot () {
if (this.$extras.pivot_course_id) {
return {
course_id: this.$extras.pivot_course_id
}
}
}
} |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
It's back to showing the pivot field by default, is it a bug ? |
I have a belongsToMany relationship, but I'd like to hide the "pivot" attribute of the output in JSON.
The text was updated successfully, but these errors were encountered: