doupin1073 2017-05-16 00:39
浏览 16
已采纳

Laravel:如何在自己的范围内列出每个帖子标签?

I need to edit a tiny piece of code on a laravel based website, but I'm not familiar with laravel syntax, so I don't know how to do this. Basically, there is a foreach loop that lists news articles. Inside of each article there is a p tag with a class "news-entry-meta", which contains two span tags, the first one displaying a post date, and the second one listing tags. My problem is that tags should not be listed inside a single span, but instead each tag should be listed inside its own span with classes "badge badge-pill badge-default". How do I do this?

    @foreach ($news as $n)
    <article class="news-entry grid-item col-12 col-md-6 col-lg-4 {{$n->tagsList}}" data-tags="{{$n->tagsList}}">
        <a href="{{URL::to('/')}}/news/{{str_slug($n->title, '-')}}" class="news-entry-featured">
        <img src="{{ $n->getThumbnail(720, 360) }}" class="news-entry-featured-img img-fluid" alt="image">
      </a>
      <div class="news-entry-text">
        <p class="news-entry-meta d-flex justify-content-end align-items-center">
          <span class="mr-auto">{{ Carbon\Carbon::parse($n->created_at)->format('d M Y') }}</span>
          <span class="badge badge-pill badge-default">{{$n->tagsList}}</span>
        </p>
        <h3 class="news-entry-title">{{$n->title}}</h3>
        <p class="news-entry-excerpt">{{str_limit($n->description, 100, '...')}}</p>
        <a href="{{URL::to('/')}}/news/{{str_slug($n->title, '-')}}" class="news-entry-more">read more</a>
      </div>
    </article>
    @endforeach
  • 写回答

1条回答 默认 最新

  • dongmei425373 2017-05-16 01:31
    关注

    this should work if $n->tagList is a string with spaces between as you mentioned in a comment.

      @foreach ($news as $n)
        <article class="news-entry grid-item col-12 col-md-6 col-lg-4 {{$n->tagsList}}" data-tags="{{$n->tagsList}}">
            <a href="{{URL::to('/')}}/news/{{str_slug($n->title, '-')}}" class="news-entry-featured">
            <img src="{{ $n->getThumbnail(720, 360) }}" class="news-entry-featured-img img-fluid" alt="image">
          </a>
          <div class="news-entry-text">
            <p class="news-entry-meta d-flex justify-content-end align-items-center">
              <span class="mr-auto">{{ Carbon\Carbon::parse($n->created_at)->format('d M Y') }}</span>
                @foreach(explode(' ',$n->tagsList) as $tag)
              <span class="badge badge-pill badge-default">{{$tag}}</span>
                @endforeach
            </p>
            <h3 class="news-entry-title">{{$n->title}}</h3>
            <p class="news-entry-excerpt">{{str_limit($n->description, 100, '...')}}</p>
            <a href="{{URL::to('/')}}/news/{{str_slug($n->title, '-')}}" class="news-entry-more">read more</a>
          </div>
        </article>
        @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题