如何清理这个Livewire组件?

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

How could I clean up this livewire component?

问题

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

  1. <div>
  2. <select name="tags[]" id="tags" multiple
  3. class="select2 mt-1 w-full block rounded-md bg-gray-100 border-transparent hover:border-gray-500 hover:bg-white hover:ring-0">
  4. @if (old('tags'))
  5. @foreach (old('tags') as $oldtag)
  6. <option value="{{ $oldtag }}" selected>{{ $oldtag }}</option>
  7. @endforeach
  8. @else
  9. @isset($post)
  10. @if ($post->tags()->count() > 0)
  11. @foreach ($post->tags as $singleTag)
  12. <option value="{{ $singleTag->text }}" selected>{{ $singleTag->text }}</option>
  13. @endforeach
  14. @endif
  15. @endisset
  16. @endif
  17. </select>
  18. @if ($errors->has('tags'))
  19. <small class="text-red-600">{{ $errors->first('tags') }}</small>
  20. @endif
  21. </div>
  22. **select2 script init below inside @push('scripts')**

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

英文:

The code:

  1. &lt;div&gt;
  2. &lt;select name=&quot;tags[]&quot; id=&quot;tags&quot; multiple
  3. 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;
  4. @if (old(&#39;tags&#39;))
  5. @foreach (old(&#39;tags&#39;) as $oldtag)
  6. &lt;option value=&quot;{{ $oldtag }}&quot; selected&gt;{{ $oldtag }}&lt;/option&gt;
  7. @endforeach
  8. @else
  9. @isset($post)
  10. @if ($post-&gt;tags()-&gt;count() &gt; 0)
  11. @foreach ($post-&gt;tags as $singleTag)
  12. &lt;option value=&quot;{{ $singleTag-&gt;text }}&quot; selected&gt;{{ $singleTag-&gt;text }}&lt;/option&gt;
  13. @endforeach
  14. @endif
  15. @endisset
  16. @endif
  17. &lt;/select&gt;
  18. @if ($errors-&gt;has(&#39;tags&#39;))
  19. &lt;small class=&quot;text-red-600&quot;&gt;{{ $errors-&gt;first(&#39;tags&#39;) }}&lt;/small&gt;
  20. @endif
  21. &lt;/div&gt;
  22. **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 计算属性中:

  1. public function getTagOptionsProperty() {
  2. $old = request()->old('tags');
  3. return $old ?? $this->post->tags()->pluck('text')->toArray();
  4. }

然后,在你的 Blade 视图中:

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

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

英文:

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

  1. public function getTagOptionsProperty() {
  2. $old = request()-&gt;old(&#39;tags&#39;);
  3. return $old ?? $this-&gt;post-&gt;tags()-&gt;pluck(&#39;text&#39;)-&gt;toArray();
  4. }

Then, inside of your Blade view:

  1. @foreach($this-&gt;tagOptions)
  2. {{-- ... --}}
  3. @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:

确定