Skip to content

Commit

Permalink
feat: added support for morph to many relationships
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Frey <[email protected]>
  • Loading branch information
lukas-frey committed Feb 17, 2023
1 parent 28c951e commit 6f7d894
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Concerns/Pipe/RelatedPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

Expand All @@ -25,6 +26,7 @@ protected function related(Collection $data): Collection
foreach ($this->memory->all() as $table => $relations) {

foreach ($relations as $name => $relation) {

if ($relation['relation'] === HasOneOrMany::class) {
$bundle = Bundle::make($relation['related']);
$bundle->populator = $this->bundle->populator;
Expand All @@ -33,6 +35,15 @@ protected function related(Collection $data): Collection
$processor->process($relation['record'], $name);
}

if ($relation['relation'] === MorphToMany::class) {
DB::table($table)
->insert([
$relation['foreign']['pivot_key'] => $id,
$relation['foreign']['morph_type'] => $this->bundle->model::class,
$relation['related']['pivot_key'] => $relation['related']['id'],
]);
}

if ($relation['relation'] === BelongsToMany::class) {
DB::table($table)
->insert([
Expand Down
34 changes: 34 additions & 0 deletions src/Concerns/Pipe/Relations/MorphRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Guava\LaravelPopulator\Concerns\Pipe\Relations;

use Guava\LaravelPopulator\Exceptions\InvalidBundleException;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Str;

trait MorphRelations
Expand Down Expand Up @@ -85,4 +87,36 @@ protected function morphOneOrMany(MorphOneOrMany $relation, array $records): voi
$index++;
}
}

/**
* Processes the belongs to many relationship and queues the relation for creation.
*
* @param MorphToMany $relation
* @param array $value
* @return void
* @throws InvalidBundleException
*/
protected function morphToMany(MorphToMany $relation, array $value): void
{
foreach ($value as $identifier) {
$id = $this->getPrimaryId($relation->getRelated(), $identifier);

if (!$id) {
$bundleName = $this->bundle->model::class;
throw new InvalidBundleException("Item {$this->name} from Sample {$bundleName} has an invalid belongsToMany relation set for {$relation->getRelationName()} (value: {$identifier}).");
}

$this->memory->set($relation->getTable(), $identifier, [
'relation' => $relation::class,
'foreign' => [
'pivot_key' => $relation->getForeignPivotKeyName(),
'morph_type' => $relation->getMorphType(),
],
'related' => [
'pivot_key' => $relation->getRelatedPivotKeyName(),
'id' => $id,
],
]);
}
}
}
5 changes: 5 additions & 0 deletions src/Concerns/Pipe/RelationsPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Collection;

trait RelationsPipe
Expand Down Expand Up @@ -47,6 +48,10 @@ protected function relations(Collection $data): Collection
$this->morphMany($relation, $value);
return [];

case $relation instanceof MorphToMany:
$this->morphToMany($relation, $value);
return [];

case $relation instanceof HasOne:
$this->hasOne($relation, $value);
return [];
Expand Down

0 comments on commit 6f7d894

Please sign in to comment.