diff --git a/src/Concerns/Pipe/RelatedPipe.php b/src/Concerns/Pipe/RelatedPipe.php index 9525be3..2661e9c 100644 --- a/src/Concerns/Pipe/RelatedPipe.php +++ b/src/Concerns/Pipe/RelatedPipe.php @@ -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; @@ -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; @@ -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([ diff --git a/src/Concerns/Pipe/Relations/MorphRelations.php b/src/Concerns/Pipe/Relations/MorphRelations.php index 2149cbd..6cfee73 100644 --- a/src/Concerns/Pipe/Relations/MorphRelations.php +++ b/src/Concerns/Pipe/Relations/MorphRelations.php @@ -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 @@ -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, + ], + ]); + } + } } diff --git a/src/Concerns/Pipe/RelationsPipe.php b/src/Concerns/Pipe/RelationsPipe.php index cee78d7..0116768 100644 --- a/src/Concerns/Pipe/RelationsPipe.php +++ b/src/Concerns/Pipe/RelationsPipe.php @@ -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 @@ -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 [];