douqinlu4217 2013-10-31 05:33
浏览 22

Laravel 4:使用不同键的刀片模板@foreach

Right now, I have to create lots of blade templates because of the following problem. I am seeking creative solutions for this issue. One of them is to do a 'select as' query but Eloquent Model::with() does not support aliasing (as far as I can tell).

@foreach($products as $product)
    @foreach($product->typeAs as $typeA) or @foreach($product->typeBs as $typeB)
        // same html for both typeA and typeB
    @endforeach
@endforeach
  • 写回答

1条回答 默认 最新

  • doumang20060820 2013-10-31 05:56
    关注

    You may want to use an @include and pass a sub-view with a variable named the same for both cases:

    @foreach($product->typeAs as $typeA)
        @include('view.name', array('type'=> $typeA))
    @endforeach
    
    @foreach($product->typeBs as $typeB)
        @include('view.name', array('type'=> $typeB))
    @endforeach
    

    You may refer to the docs as well

    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部