What I'm trying to do is passing 2 extra values on my belongsToMany relation that should be placed on the pivot.
I've read the documentation and found this: https://lighthouse-php.com/6/eloquent/nested-mutations.html#belongstomany
I've tried to replicate it and it works with 1 value. But I'd like to pass 2 values and when I try that it says that for the second value there is no default value. I can't seem te figure out what is going wrong in my mutation...
type AdvancedTreatment {
id: ID!
createdAt: DateTime! @rename(attribute: "created_at")
updatedAt: DateTime! @rename(attribute: "updated_at")
deletedAt: DateTime @rename(attribute: "deleted_at")
visit: Visit! @belongsTo
advancedParameters: [AdvancedParameter] @hasMany
advancedMedications: [AdvancedMedication!] @belongsToMany
advancedSampleEvaluation: AdvancedSampleEvaluation @hasOne
advancedAppliedActions: [AdvancedAppliedAction] @hasMany
pivot: AdvancedMedicationAdvancedTreatmentPivot!
}
type AdvancedMedicationAdvancedTreatmentPivot{
advancedAdministrationType: AdvancedAdministrationType!
administeredDose: String! @rename(attribute: "administered_dose")
}
extend type Mutation {
saveAdvancedMedications(input: SaveAdvancedMedicationsInput! @spread): AdvancedTreatment! @update
}
input SaveAdvancedMedicationsInput {
id: ID!
advancedMedications: UpdateAdvancedMedicationsBelongsToMany
}
input UpdateAdvancedMedicationsBelongsToMany {
sync: [SyncAdvancedMedicationAdvancedTreatment!]
}
input SyncAdvancedMedicationAdvancedTreatment {
id: ID!
advancedAdministrationType: AdvancedAdministrationType! @rename(attribute: "advanced_administration_type")
administeredDose: String @rename(attribute: "administered_dose")
}
enum AdvancedAdministrationType{
IV @enum(value: "iv")
IM @enum(value: "im")
PO @enum(value: "po")
SC @enum(value: "sc")
AERO @enum(value: "aero")
RECTAL @enum(value: "rect")
TRANSD @enum(value: "transd")
NASAL @enum(value: "nasal")
}
My mutation in graphiql:
mutation{
saveAdvancedMedications(input:{
id: 1
advancedMedications:{
sync: [
{
id:1
advancedAdministrationType: IV
administeredDose: "1 Dick/gr"
}
{
id:2
advancedAdministrationType: IM
administeredDose: "2 Dick/gr"
}
]
}
}){
id
advancedMedications{
id
pivot{
advancedAdministrationType
administeredDose
}
}
}
}
It just looks like it won't take my advancedAdministrationType into the query for some reason? is there something that I'm missing or doing
EDIT: What I already foudn is that it looks like when using a Pivot model Lighthouse won't let it work
public function advancedMedications(): BelongsToMany
{
return $this->belongsToMany(AdvancedMedication::class)
->using(AdvancedMedicationAdvancedTreatment::class)
->withPivot(['administered_dose', 'advanced_administration_type'])
->withTimestamps();
}
So When I remove on both sides of the relation
->using(AdvancedMedicationAdvancedTreatment::class)
it will magically work. Is it because I'm missing some properties on the pivot model or because of Lighthouse won't work with pivot models? Hereby my pivot model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Pivot;
class AdvancedMedicationAdvancedTreatment extends Pivot
{
protected $fillable = [
'administered_dose',
'advanced_administrations_type',
];
/**
* Belongs to AdvancedMedication.
*/
public function advancedMedication(): BelongsTo
{
return $this->belongsTo(AdvancedMedication::class);
}
/**
* Belongs to AdvancedTreatment.
*/
public function advancedTreatment(): BelongsTo
{
return $this->belongsTo(AdvancedTreatment::class);
}
}
SOLUTION Remove the fillables solved it all for some reason:
protected $fillable = [
'administered_dose',
'advanced_administrations_type',
];
After hours of debugging I found that on a Pivot model you have to remove the fillables to make this work:
In this case I had to remove SOLUTION Remove the fillables solved it all for some reason:
protected $fillable = [
'administered_dose',
'advanced_administrations_type',
];