| @@ 30-56 (lines=27) @@ | ||
| 27 | * @return void |
|
| 28 | * @throws \Exception |
|
| 29 | */ |
|
| 30 | public function attach($id, array $attributes = [], $touch = true) |
|
| 31 | { |
|
| 32 | parent::attach($id, $attributes, $touch); |
|
| 33 | ||
| 34 | if (! in_array(get_class($this->parent), NovaTranslation::translatableModels())) { |
|
| 35 | return; |
|
| 36 | } |
|
| 37 | ||
| 38 | if ($this->parent->isTranslatingRelation()) { |
|
| 39 | return; |
|
| 40 | } |
|
| 41 | ||
| 42 | $keys = $this->getTranslatedKeys($this->parseIds($id), $locales = NovaTranslation::otherLocales()); |
|
| 43 | ||
| 44 | $this->parent->translations() |
|
| 45 | ->with(['locale', 'translatable']) |
|
| 46 | ->get() |
|
| 47 | ->each(function (Translation $translation) use ($keys, $attributes, $touch) { |
|
| 48 | $model = $translation->translatable; |
|
| 49 | $model->translatingRelation(); |
|
| 50 | $model->{$this->relationName}()->attach( |
|
| 51 | data_get($keys, $translation->locale->iso, []), |
|
| 52 | $attributes, |
|
| 53 | $touch |
|
| 54 | ); |
|
| 55 | }); |
|
| 56 | } |
|
| 57 | ||
| 58 | public function detach($ids = null, $touch = true) |
|
| 59 | { |
|
| @@ 58-82 (lines=25) @@ | ||
| 55 | }); |
|
| 56 | } |
|
| 57 | ||
| 58 | public function detach($ids = null, $touch = true) |
|
| 59 | { |
|
| 60 | $result = parent::detach($ids, $touch); |
|
| 61 | ||
| 62 | if (! in_array(get_class($this->parent), NovaTranslation::translatableModels())) { |
|
| 63 | return $result; |
|
| 64 | } |
|
| 65 | ||
| 66 | if ($this->parent->isTranslatingRelation()) { |
|
| 67 | return $result; |
|
| 68 | } |
|
| 69 | ||
| 70 | $keys = $this->getTranslatedKeys($this->parseIds($ids), NovaTranslation::otherLocales()); |
|
| 71 | ||
| 72 | $this->parent->translations() |
|
| 73 | ->with(['locale', 'translatable']) |
|
| 74 | ->get() |
|
| 75 | ->each(function (Translation $translation) use ($keys, $touch) { |
|
| 76 | $model = $translation->translatable; |
|
| 77 | $model->translatingRelation(); |
|
| 78 | $model->{$this->relationName}()->detach(data_get($keys, $translation->locale->iso, []), $touch); |
|
| 79 | }); |
|
| 80 | ||
| 81 | return $result; |
|
| 82 | } |
|
| 83 | ||
| 84 | public function sync($ids, $detaching = true) |
|
| 85 | { |
|
| @@ 84-108 (lines=25) @@ | ||
| 81 | return $result; |
|
| 82 | } |
|
| 83 | ||
| 84 | public function sync($ids, $detaching = true) |
|
| 85 | { |
|
| 86 | $changes = parent::sync($ids, $detaching); |
|
| 87 | ||
| 88 | if (! in_array(get_class($this->parent), NovaTranslation::translatableModels())) { |
|
| 89 | return $changes; |
|
| 90 | } |
|
| 91 | ||
| 92 | if ($this->parent->isTranslatingRelation()) { |
|
| 93 | return $changes; |
|
| 94 | } |
|
| 95 | ||
| 96 | $keys = $this->getTranslatedKeys($this->parseIds($ids), NovaTranslation::otherLocales()); |
|
| 97 | ||
| 98 | $this->parent->translations() |
|
| 99 | ->with(['locale', 'translatable']) |
|
| 100 | ->get() |
|
| 101 | ->each(function (Translation $translation) use ($keys, $detaching) { |
|
| 102 | $model = $translation->translatable; |
|
| 103 | $model->translatingRelation(); |
|
| 104 | $model->{$this->relationName}()->sync(data_get($keys, $translation->locale->iso, []), $detaching); |
|
| 105 | }); |
|
| 106 | ||
| 107 | return $changes; |
|
| 108 | } |
|
| 109 | ||
| 110 | public function getTranslatedKeys(array $keys, Collection $locales): array |
|
| 111 | { |
|