Skip to content
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

Dealing with attributes on the pivot table of a BelongsToMany relation #691

Closed
TheJoeSchr opened this issue Mar 28, 2019 · 5 comments
Closed
Labels
docs Concerns the documentation enhancement A feature or improvement

Comments

@TheJoeSchr
Copy link

Situation

I'm trying to implement @update on a belongsToMany relationship. The pivot table also has columns I want to update, like this:

class Patient extends Model
{
    // many to many RELATIONSHIPS
    public function medications(): BelongsToMany
    {
        return $this->belongsToMany(Medication::class, 'patients__medications', 'patient_id', 'medication_id')->withPivot('dose_mg', 'is_rescue_medication'); //->as("details");
    }
}

my example schema:

#schema.graphql excerpts
# Mutations {}
updateMedication(input: MedicationInput): Medication! @update(flatten: true)

# Types
# -- Medication
input MedicationInput {
    id: ID
    name: String
    pivot: MedicationDetailsInput
}

input MedicationDetailsInput {
    patient_id: ID
    medication_id: ID
    dose_mg: Float
    is_rescue_medication: Boolean
}
input MedicationRelation {
  update: [MedicationInput]
  connect: [MedicationDetailsInput]
  disconnect: [ID]
  create: [MedicationInput]
  delete: [MedicationInput]
  sync: [MedicationDetailsInput]
}

Problem

I get an SQL error that laravel can't find the columns (of course, they don't exist because they are on the pivot table) on the related table to update.

I debugged it down to the executeUpdate function in MutationExecutor.php , where the pivot property is still inside $remaining and gets treated as a normal property on the related Model instead of the special case of being pivot columns.

Expected

pivot columns should be updated,

Workarounds

there was a workaround suggested:

You can treat your BelongsTo relation simply as two BelongsTo relationships to work around this.

by @spawnia in this issue thread #549

@spawnia spawnia added the enhancement A feature or improvement label Mar 28, 2019
@spawnia spawnia changed the title Allow pivot columns on a belongsToMany @update nested mutation Nested mutations for pivot attributes of a BelongsToMany relation Apr 8, 2019
@spawnia
Copy link
Collaborator

spawnia commented Apr 8, 2019

For future-reference, here is an excerpt from the discussion of #692:

When you think about what the BelongsToMany relationship actually is, you will find that it is in fact comprised of a HasMany and a BelongsTo relationship.

In terms of your concrete example, you should be able to update a medication like this:

mutation {
	updateMedication(input: {
		id: 1
		name: "Lysergicream"
		# That would be a HasMany relationship from Mediation to the pivot model
		medicationUser: {
			update: [{
				dose_mg: 11
				# This is a belongsTo relationship from your pivot model to User
				user: {
                    connect: 1
                }
           }]
        }
    }) {
        id
        name
        # Will return a list, as this is a HasMany relationship
        medicationUser {
            dose_mg
            # A single BelongsTo
            user {
                 id
                 name
            }
         }
    }
}

I think we should add a section about how to deal with pivot attributes to the docs. Queries are affected too and should be considered.

@spawnia spawnia changed the title Nested mutations for pivot attributes of a BelongsToMany relation Dealing with attributes on the pivot table of a BelongsToMany relation Apr 8, 2019
@spawnia spawnia added the docs Concerns the documentation label Apr 8, 2019
@spawnia
Copy link
Collaborator

spawnia commented Jan 12, 2020

@JoeSchr you can now use nested mutations to do this directly and in a convenient way: #1110

@spawnia spawnia closed this as completed Jan 12, 2020
@TheJoeSchr
Copy link
Author

Thanks. Meanwhile I made my custom fork to work around this, but it seems it's finally time to update to this release!

@AbdElrahmaN31
Copy link

AbdElrahmaN31 commented Dec 2, 2021

@spawnia
How can I write a type that expresses this relationship (beongsToMany + Pivot columns)?

@spawnia
Copy link
Collaborator

spawnia commented Dec 2, 2021

You can use the following channels to ask support questions:

This repository and the issue tracker are used to advance the development of Lighthouse.

@nuwave nuwave locked as resolved and limited conversation to collaborators Dec 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
docs Concerns the documentation enhancement A feature or improvement
Projects
None yet
Development

No branches or pull requests

3 participants