2016-03-02 133 views
0

即时通讯开发一个博客,其中所有文章可以有多个类别,并且我希望这些类别是来自div的类属性,因为即时通讯使用数据过滤器来显示这些内容发布您从类别菜单中选择并隐藏那些不属于该类别的人员。Laravel 5.2 Blade在“类属性”中迭代

即时通讯使用下一个代码,但如果您检查{{$post->categorias[0]->nombre}}(其中categorias是迭代的类别部分,“nombre”是类别的名称)只会显示第一个类别,我想显示属于所有类别到那个职位,不仅是第一个。

@foreach($posts as $post) 
<figure class="{{$post->categorias[0]->nombre}}"> 
    <a href="project.html" class="thumb"> 
    @foreach($post->imagenes as $imagen) 
    <img src="/imagenes/articulos/{{$imagen->url}}" alt="alt"/> 
    @endforeach 
    </a> 

    <figcaption> 
    <a href="project.html" ><h3 class="heading">{{$post->nombre}}</h3></a> 
    {{$post->contenido}}</figcaption> 
</figure> 
@endforeach 

我已经尝试过迭代里面class=" - "但没有工作,是这样的:

<figure class="@foreach ($post->categorias as $po) 
        {{$po->nombre}} 
       @endforeach"> 

含苞未工作

我会明白任何帮助。谢谢

+0

你已经错过了'{{'从'$宝>农布雷}开始}'在你的榜样。如果你的代码也是这样,那么它将无法工作。 – Joseph

回答

0

BLADE

@foreach($posts as $key => $post) 
<figure class="{{$post->categorias[$key]->nombre}}"> 
    <a href="project.html" class="thumb"> 
    @foreach($post->imagenes as $imagen) 
    <img src="/imagenes/articulos/{{$imagen->url}}" alt="alt"/> 
    @endforeach 
    </a> 

    <figcaption> 
    <a href="project.html" ><h3 class="heading">{{$post->nombre}}</h3></a> 
    {{$post->contenido}}</figcaption> 
</figure> 
@endforeach 

$key是 '反',以得到正确的数据。

+0

好吧,似乎是它的答案,但现在我得到一个未定义的抵消错误...要检查...给我一分钟 –

+0

得到答案感谢你,但它不是那样 –

+0

我只是回答我的问题,但这是对你的感谢。 :) –

0
@foreach($posts as $post) 
<figure class="@foreach($post->categorias as $key => $po) 
    {{$post->categorias[$key]->nombre}}@endforeach"> 
     <a href="project.html" class="thumb"> 
     @foreach($post->imagenes as $imagen) 
     <img src="/imagenes/articulos/{{$imagen->url}}" alt="alt"/> 
     @endforeach 
     </a> 

    <figcaption> 
     <a href="project.html" ><h3 class="heading">{{$post->nombre}}</h3></a> 
     {{$post->contenido}}</figcaption> 
</figure> 
@endforeach 

感谢@djl的帮助:)