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

Adding support to fill pivot data in many-to-many relations #19

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Mina-R-Meshriky
Copy link

@Mina-R-Meshriky Mina-R-Meshriky commented Sep 16, 2021

issue laravel-json-api/laravel#114
if in the request of many-to-many the relationship data contained mata.pivot it will be inserted in the pivot table

'relationships' => [
    'someRelation' => [
        'data' =>  [
          [
            'type' => 'relation-type',
            'id' => 4,
            'meta' => [
                'pivot' => [
                    'someName' => 'value'
                ]
           ]
         ]
        ],
    ],
],

in schema:

BelongsToMany('relationName')
->fields([
     'someName',
     'otherField' => false // default value but will be overridden if submitted in the request
])
 // all fields will default to the convention underscore
->fieldsColumns([
     'someName' => 'database_name'
])

for validation:

[
    'someRelation.*.meta.pivot.someName' => ['required']
]

@lindyhopchris
Copy link
Contributor

Thanks for submitting this. I'm going to have to take a detailed look at it and add tests before I'm happy to merge. I've been really short on open source time recently, but am starting to pick things up again. So hopefully I can get to this sometime in the next month.

@CodeNinja1337
Copy link

Great feature! this is exactly where i was looking for!

@CodeNinja1337
Copy link

@lindyhopchris , any update on this? Because i want to use it but only if its supported by the package. Not as long as its only a open pull request.

@lindyhopchris
Copy link
Contributor

Yeah I've thought about this a bit and my preference would be to do this via existing JSON:API semantics. I believe the spec gives us everything we need to do pivots via intermediary resources. Which also comes with the advantage of the client being able to control things via include paths and sparse fieldsets - which we don't get if we do it via a custom implementation that uses meta.

However, I need to sit down and work my way through a scenario and check that it all works. And if it does then I need to write it up as the definitive way to do it, so that I stop getting constant questions about pivots!! Unfortunately none of the production apps that I have using Laravel JSON:API use pivot values anywhere, which is why I've been slow to write something up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants