【发布时间】:2021-04-05 09:07:32
【问题描述】:
我想在每个类别之后加上逗号,如下所示: 测试,测试2,测试3
<p>
@foreach($article->categories()->get() as $categoris)
{{$categoris->title}}
@endforeach
</p>
我试过这个:
<p>
@foreach($article->categories()->get() as $categoris)
{{str_replace(' ' , ',' , $categoris->title)}}
@endforeach
</p>
但它并没有像我想要的那样工作。 输出是: 测试,测试2测试3
【问题讨论】:
-
$categoris->title的值是多少? -
测试测试2测试3
-
每次迭代中的类别是什么?
-
首先把
@去掉,这样就可以显示错误了(这是错误控制操作符)。在所有情况下,这是带有嵌入式 php 的 html,到处都是简写,真是一团糟。自己写,纯php,不要抄袭,使用模板引擎,这是学习的关键。不要混合逻辑和模板。 -
@ThamerAlluqmani 如果
$categoris->title是test test 2 test 3,str_replace应该如何知道哪些空格重要,哪些不重要?