如何清理这个Livewire组件?

huangapple go评论51阅读模式
英文:

How could I clean up this livewire component?

问题

以下是您要翻译的代码部分:

<div>
    <select name="tags[]" id="tags" multiple
        class="select2 mt-1 w-full block rounded-md bg-gray-100 border-transparent hover:border-gray-500 hover:bg-white hover:ring-0">
        @if (old('tags'))
            @foreach (old('tags') as $oldtag)
                <option value="{{ $oldtag }}" selected>{{ $oldtag }}</option>
            @endforeach
        @else
            @isset($post)
                @if ($post->tags()->count() > 0)
                    @foreach ($post->tags as $singleTag)
                        <option value="{{ $singleTag->text }}" selected>{{ $singleTag->text }}</option>
                    @endforeach
                @endif
            @endisset
        @endif
    </select>
    @if ($errors->has('tags'))
        <small class="text-red-600">{{ $errors->first('tags') }}</small>
    @endif
</div>

**select2 script init below inside @push('scripts')**

这是您提供的代码部分的翻译,不包含其他内容。

英文:

The code:

&lt;div&gt;
    &lt;select name=&quot;tags[]&quot; id=&quot;tags&quot; multiple
        class=&quot;select2 mt-1 w-full block rounded-md bg-gray-100 border-transparent hover:border-gray-500 hover:bg-white hover:ring-0&quot;&gt;
        @if (old(&#39;tags&#39;))
            @foreach (old(&#39;tags&#39;) as $oldtag)
                &lt;option value=&quot;{{ $oldtag }}&quot; selected&gt;{{ $oldtag }}&lt;/option&gt;
            @endforeach
        @else
            @isset($post)
                @if ($post-&gt;tags()-&gt;count() &gt; 0)
                    @foreach ($post-&gt;tags as $singleTag)
                        &lt;option value=&quot;{{ $singleTag-&gt;text }}&quot; selected&gt;{{ $singleTag-&gt;text }}&lt;/option&gt;
                    @endforeach
                @endif
            @endisset
        @endif
    &lt;/select&gt;
    @if ($errors-&gt;has(&#39;tags&#39;))
        &lt;small class=&quot;text-red-600&quot;&gt;{{ $errors-&gt;first(&#39;tags&#39;) }}&lt;/small&gt;
    @endif
&lt;/div&gt;

**select2 script init below inside @push(&#39;scripts&#39;)**

It is a livewire component that takes in an optional parameter named $post.
The reason I have else-isset-if is because, if you "edit" a post, the $post parameter gets that post object but if you "create" a new post, there's no post object to load up in the component. That way laravel still tries to read $post-&gt;tags()-&gt;count() which will throw an error stating "null" has no method called tags().

That's why I've added the isset part.

It works but also looks extremely ugly. I have absolutely no idea what to do with it.

答案1

得分: 1

你可能考虑将你的条件逻辑迁移到一个 Livewire 计算属性中:

public function getTagOptionsProperty() {
  $old = request()->old('tags');

  return $old ?? $this->post->tags()->pluck('text')->toArray();
}

然后,在你的 Blade 视图中:

@foreach($this->tagOptions)
  {{-- ... --}}
@endforeach

你的标记似乎混合了 Blade 属性和 Livewire 属性,所以我不太确定这里发生了什么,但似乎有很好的重构机会。

英文:

You might consider migrating your conditional logic into a Livewire computed property instead:

public function getTagOptionsProperty() {
  $old = request()-&gt;old(&#39;tags&#39;);

  return $old ?? $this-&gt;post-&gt;tags()-&gt;pluck(&#39;text&#39;)-&gt;toArray();
}

Then, inside of your Blade view:

@foreach($this-&gt;tagOptions)
  {{-- ... --}}
@endforeach

Your markup seems to be mixing Blade attributes with Livewire properties, so I'm not entirely sure what's going on here, but there would seem to be a good opportunity for refactoring.

huangapple
  • 本文由 发表于 2023年2月10日 03:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定