App\Repositories\PostRepository::syncTags PHP Method

syncTags() private method

Synchronize post tags relation.
private syncTags ( $post, string $tags )
$post
$tags string
    private function syncTags($post, $tags)
    {
        $tags_id = [];
        $tags = explode(',', $tags);
        foreach ($tags as $tag) {
            $tag_ref = $this->tag->whereName($tag)->first();
            if (is_null($tag_ref)) {
                $tag_ref = new $this->tag();
                $tag_ref->name = $tag;
                $tag_ref->save();
            }
            array_push($tags_id, $tag_ref->id);
        }
        $post->tags()->sync($tags_id);
    }