comic['list'] as $comic) { $comicsUpsertArray[] = [ 'pathword' => $comic['path_word'], 'uuid' => '', 'name' => $comic['name'], 'alias' => '{}', 'description' => '', 'cover' => $comic['cover'], 'upstream_updated_at' => $comic['datetime_updated'] ?? null, ]; foreach ($comic['author'] as $author) { $authorsUpsertArray[] = [ 'name' => $author['name'] ]; } } // Do an upsert for comics Comic::upsert($comicsUpsertArray, uniqueBy: 'pathword', update: ['upstream_updated_at']); Author::upsert($authorsUpsertArray, uniqueBy: 'name'); // Had to do a second pass to insert the relationships foreach ($this->comic['list'] as $comic) { // Get the comic id $comicObj = Comic::where('pathword', $comic['path_word'])->first(); foreach ($comic['author'] as $author) { $authorObj = Author::where('name', $author['name'])->first(); $comicObj->authors()->sync($authorObj); } } Log::info('JOB ComicUpsert END'); } }