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

Use relatedKey instead of getKey in order to manage not-standard belongsTomany #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

alberto-bottarini
Copy link
Contributor

@alberto-bottarini alberto-bottarini commented Dec 29, 2020

I've got this scenario.

Two models:

  • Rule
  • Persona

A Rule has a belongsToMany relationship with Persona:

public function personas()
{
  return $this->belongsToMany(Persona::class, 'feature_rule_personas', 'rule_id', 'persona_code', 'id', 'code');
}

This is the pivot table structure:

id, rule_id, persona_code

I would like to have in the pivot table not the Persona ->getKey but a custom key, in this case the code column: Laravel will allow me to do this using some extra parameter in belongsToMany.

Without this PR, plugin will use sync method using id column and not the right code column, inserting wrong data in database.

I added a $resource->{$parentModel->$relationship()->getRelatedKeyName()} in order to get the right relatedKey.

@alberto-bottarini alberto-bottarini changed the title Add custom key to manage not-standard belongs2many Add custom key to manage not-standard belongsTomany Dec 29, 2020
@alberto-bottarini alberto-bottarini changed the title Add custom key to manage not-standard belongsTomany Use relatedKey instead of getKey in order to manage not-standard belongsTomany Dec 29, 2020
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.

None yet

1 participant