Currently I have an input in which the user is meant to write tags and separate them with commas. Then on the back-end the string is exploded and each tag is saved into the database. However, if I don't write any tags, a tag with name of empty string is saved into the database. How can I avoid that?
HTML
<div class="form-group">
<label class='label' for="artwork-tags">Tags</label>
<input class='input' type="text" name="artwork-tags" placeholder="Tags" value='{{ Request::old('artwork-tags') }}'>
@include('partials.invalid', ['field' => 'artwork-tags'])
</div>
PHP
$tagsRaw = $request->input('artwork-tags');
$tags = explode(',', $tagsRaw);
foreach($tags as $tagName) {
$tagExists = Tag::where('name', $tagName)->exists();
if (!$tagExists) {
$tag = new Tag();
$tag->name = $tagName;
$tag->save();
$image->tags()->attach($tag);
} else {
$existingTag = Tag::where('name', $tagName)->first();
$image->tags()->attach($existingTag);
}
}
$tagsRawis not empty.