Skip to content

Commit

Permalink
relationship option for pivot
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdekruijk committed Nov 24, 2023
1 parent 0e7e2da commit 315e06a
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Controllers/ModelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ private function save($model, Request $request)
$morph = [];
$row = [];
$using = [];
$relationship = [];
foreach ($this->columns() as $columnId => $column) {
if (isset($column['type']) && $column['type'] == 'pivot') {
$sync[$column['model']] = $request[$columnId];
$using[$column['model']] = $column['using'] ?? null;
$relationship[$column['model']] = $column['relationship'] ?? null;
if (!empty($column['morph'])) {
$morph[$column['model']] = $column['morph'];
}
Expand Down Expand Up @@ -87,6 +89,8 @@ private function save($model, Request $request)
foreach ($sync as $foreign => $values) {
if (isset($morph[$foreign])) {
$model->morphToMany($foreign, $morph[$foreign])->sync($values);
} elseif (isset($relationship[$foreign])) {
$model->{$relationship[$foreign]}()->sync($values);
} else {
$model->belongsToMany($foreign)->using($using[$foreign])->sync($values);
}
Expand Down Expand Up @@ -156,6 +160,8 @@ public function show($slug, $id)
$ids = [];
if (!empty($column['morph'])) {
$pivotData = $this->model()::findOrFail($id)->morphToMany($column['model'], $column['morph'])->get();
} elseif (isset($column['relationship'])) {
$pivotData = $this->model()::findOrFail($id)->{$column['relationship']};
} else {
$pivotData = $this->model()::findOrFail($id)->belongsToMany($column['model'])->get();
}
Expand Down

0 comments on commit 315e06a

Please sign in to comment.