movie['id']); // Genres $genres = []; $genre_ids = []; foreach ($this->movie['genres'] as $genre) { $genre_ids[] = $genre['id']; $genres[] = [ 'id' => $genre['id'], 'name' => $genre['name'] ]; } Genre::upsert($genres, 'id'); $movie->genres()->sync(array_unique($genre_ids)); // Companies $companies = []; $company_ids = []; foreach ($this->movie['production_companies'] ?? [] as $company) { $company = (new Client\Company($company['id']))->getData(); $company_ids[] = $company['id']; $companies[] = [ 'id' => $company['id'], 'description' => $company['description'] ?? null, 'headquarters' => $company['headquarters'] ?? null, 'homepage' => $company['homepage'] ?? null, 'logo' => $tmdb->image('logo', $company), 'name' => $company['name'] ?? null, 'origin_country' => $company['origin_country'], ]; } Company::upsert($companies, 'id'); $movie->companies()->sync(array_unique($company_ids)); // Collection if (isset($this->movie['belongs_to_collection']['id'])) { $collection = (new Client\Collection($this->movie['belongs_to_collection']['id']))->getData(); $titleSort = addslashes(str_replace(['The ', 'An ', 'A ', '"'], [''], $collection['name'])); $collection = [ 'id' => $collection['id'], 'name' => $collection['name'] ?? null, 'name_sort' => $titleSort, 'parts' => is_countable($collection['parts']) ? \count($collection['parts']) : 0, 'overview' => $collection['overview'] ?? null, 'poster' => $tmdb->image('poster', $collection), 'backdrop' => $tmdb->image('backdrop', $collection), ]; Collection::upsert($collection, 'id'); $movie->collection()->sync([$collection['id']]); } // People $people_ids = []; $credits = []; foreach ($this->movie['credits']['cast'] ?? [] as $person) { $credits[] = [ 'movie_id' => $this->movie['id'], 'person_id' => $person['id'], 'occupation_id' => Occupations::ACTOR->value, 'character' => $person['character'] ?? '', 'order' => $person['order'] ?? null ]; $people_ids[] = $person['id']; } foreach ($this->movie['credits']['crew'] ?? [] as $person) { $job = Occupations::from_tmdb_job($person['job']); if ($job !== null) { $credits[] = [ 'movie_id' => $this->movie['id'], 'person_id' => $person['id'], 'occupation_id' => $job->value, 'character' => null, 'order' => null ]; $people_ids[] = $person['id']; } } $people = []; foreach (array_unique($people_ids) as $person_id) { $person = (new Client\Person($person_id))->getData(); $people[] = $tmdb->person_array($person); } Person::upsert($people, 'id'); Credit::where('movie_id', '=', $this->movie['id'])->delete(); Credit::upsert($credits, ['person_id', 'movie_id', 'tv_id', 'occupation_id', 'character']); // Recommendations $movie_ids = Movie::select('id')->findMany(array_column($this->movie['recommendations']['results'] ?? [], 'id'))->pluck('id'); $recommendations = []; foreach ($this->movie['recommendations']['results'] ?? [] as $recommendation) { if ($movie_ids->contains($recommendation['id'])) { $recommendations[] = [ 'recommendation_movie_id' => $recommendation['id'], 'movie_id' => $this->movie['id'], 'title' => $recommendation['title'], 'vote_average' => $recommendation['vote_average'], 'poster' => $tmdb->image('poster', $recommendation), 'release_date' => $recommendation['release_date'], ]; } } Recommendation::upsert($recommendations, ['recommendation_movie_id', 'movie_id']); } }