public function findOrCreate(array $tags)
    {
        $foundTags = $this->findWhereIn(['tag', $tags]);

        $returnTags = [];

        if ($foundTags) {
            foreach ($foundTags as $tag) {
                $pos = array_search($tag->tag, $tags);

                // Add returned tags to array
                if ($pos !== false) {
                    $returnTags[] = $tag;
                    unset($tags[$pos]);
                }
            }
        }

        // Add remainings tags as new
        foreach ($tags as $tag) {
            $returnTags[] = $this->create([
                'tag' => $tag,
                'slug' => Str::slug($tag),
            ]);
        }

        return $returnTags;
    }
public function findOrCreate(array $tags)
    {
        $foundTags = $this->findWhereIn(['tag', $tags]);

        $returnTags = [];

        if ($foundTags) {
            foreach ($foundTags as $tag) {
                $pos = array_search($tag->tag, $tags);

                // Add returned tags to array
                if ($pos !== false) {
                    $returnTags[] = $tag;
                    unset($tags[$pos]);
                }
            }
        }

        // Add remainings tags as new
        foreach ($tags as $tag) {
            $returnTags[] = $this->create([
                'tag' => $tag,
                'slug' => Str::slug($tag),
            ]);
        }

        return $returnTags;
    }
public function findOrCreate ( array $ tags ) { $ foundTags = $ this -> findWhereIn ( [ 'tag' , $ tags ] ) ; $ returnTags = [ ] ; if ( $ foundTags ) { foreach ( $ foundTags as $ tag ) { $ pos = array_search ( $ tag -> tag , $ tags ) ; // Add returned tags to array if ( $ pos !== false ) { $ returnTags [ ] = $ tag ; unset ( $ tags [ $ pos ] ) ; } } } // Add remainings tags as new foreach ( $ tags as $ tag ) { $ returnTags [ ] = $ this -> create ( [ 'tag' => $ tag , 'slug' => Str :: slug ( $ tag ) , ] ) ; } return $ returnTags ; }
Find existing tags or create if they don't exist.

@param array $tags Array of strings, each representing a tag

@return array Array or Arrayable collection of Tag objects
Find existing tags or create if they don t exist .