Answer the question
In order to leave comments, you need to log in
How to improve the function?
Hello. There is a function that accepts a request, checks for new tags, if any, adds to the database, get the id and add everything to the common array with indexes, and finally synchronize.
How would you make this feature?
public static function getTags($requestTags): array
{
$tags = [];
if ($requestTags) {
foreach ($requestTags as $tag) {
if (is_numeric($tag)) {
$tags[] = $tag;
} else {
$newTag = Tag::upsert(['name' => $tag]);
$tags[] = $newTag->id;
}
}
}
return $tags;
}
Answer the question
In order to leave comments, you need to log in
return \collect((array)$requestTags)->map(function($tag): int {
return \is_numeric($tag) ? (int)$tag : Tag::upsert(['name' => $tag])->id;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question