Nisha Patel
Problem
I am familiar with creating a >Many To Many relationship using Laravel.
In my example, I have many resources that share many activities. I have been able to successfully create this relationship in their respective models:
I'll then use the sync method to create the relationship.
I'm attempting to create this same relationship using the >Laravel JSON:API package.
My ActivitySchema looks like this:
My ResourceSchema looks like this:
Using Postman, when creating or updating a resource, my request body looks like this:
"attributes": {
...
The error I'm currently getting when creating a new resource is:
"detail": "The field activities is not a supported attribute.",
Usually, that means I am missing the field on my Schema. I have verified that it's there, so I'm wondering if it's due to the relationship?
I have tried using a pivot model, but no success there either. Do I need to create a custom controller to handle this logic? I'm not having much luck finding any instructions on how to sync the models.
How can I create/sync the relationship using the JSON:API package?
Solution
You will want to check out >the tutorial as it has an example of a many-to-many relationship; Posts -- Tags.
In addition to the "attributes" you want to update in your resource, you'll also need to include the relationships.
For example, your resource PATCH request body will look something like this:
Now in the response that is sent back you'll see the relationship(s).
In addition to the above, you'll also need to update the resource request to allow for the toMany rule:
If you follow the relationship, you should now see the data returned correctly.
If you pass in the included query, the API will send back the requested relationship in the response.
Answered by: >Damon
Credit: >StackOverflow
Blog Links:
>How to manage the Text in the container in Django?
>Fix webapp stops working issue in Django- Python webapp
>Creating a form in Django to upload a picture from the website
>Sending Audio file from Django to Vue
>How to keep all query parameters intact when changing page in Django?
>Solved: TaskList View in Django
>Implement nested serializers in the Django rest framework
>How to filter events by month in Django?
>Sorting the restframework in Django
>Ways to access instances of models in view in order to save both forms at once in Django
>What makes index.html have such kind of name in Django?
>Fix Module Not Found during Deployment- Django
>Creating a Django with existing directories, files, etc.?
>How to Read a CSV file with PHP using cURL?