public function splitFound($tags) { // generate slugs $tags = array_map(function ($text) { $slug = $this->helper->slugify($text); return ['text' => $text, 'slug' => $slug]; }, $tags); // extract existing tags $slugs = array_map(function ($tag) { return $tag['slug']; }, $tags); $existing = $this->tag->whereIn('slug', $slugs)->get(); $existing_slugs = $existing->lists('slug'); $new_tags = array_filter($tags, function ($tag) use($existing_slugs) { return !in_array($tag['slug'], $existing_slugs); }); // create tag models out of the new ones $new_tags = array_map(function ($tag) { return $this->tag->create($tag); }, $new_tags); $tags_ids = $existing->merge($new_tags); return $tags_ids->lists('id'); }