【问题标题】:put a condition foreach laravel为每个 laravel 设置条件
【发布时间】:2021-06-27 23:28:32
【问题描述】:

我正在 Laravel 项目中寻找解决方案。

            <div class="owl-carousel" id="listcarousel">
            @foreach ($property->images as $image)
                <div class="item"><img  src="{{ RvMedia::getImageUrl($image, null, false, RvMedia::getDefaultImage()) }}" class="showfullimg" rel="{{ $loop->index }}" alt="{{ $property->name }}"></div>
            @endforeach
        </div>

我需要在 foreach 循环中放置一个条件,输出带有以下标记的 jepg 扩展名的文件:

 <div class="item"><img  src="{{ RvMedia::getImageUrl($image, null, false, RvMedia::getDefaultImage()) }}" class="showfullimg" rel="{{ $loop->index }}" alt="{{ $property->name }}"></div>

以及带有 mp4 扩展名且带有以下标签的文件:

<div class="item"><video  src="{{ RvMedia::getImageUrl($image, null, false, RvMedia::getDefaultImage()) }}" class="showfullimg" rel="{{ $loop->index }}" alt="{{ $property->name }}"></video></div>

是的,我将所有电影和照片文件保存在一张桌子上。

【问题讨论】:

  • 我们保持大拇指!祝你好运!啊...顺便说一句,您尝试了什么?请阅读how to ask a question
  • 在您的表格中添加一个字段文件扩展名,您可以使用此字段对您的特定文件类型进行分组。

标签: php laravel foreach


【解决方案1】:
@foreach($property->images as $image)
    <div class="item">
        @if(str_contains($property->name, '.jpeg'))
            <div class="item"><img  src="{{ RvMedia::getImageUrl($image, null, false, RvMedia::getDefaultImage()) }}" class="showfullimg" rel="{{ $loop->index }}" alt="{{ $property->name }}"></div>
        @else
            <div class="item"><video  src="{{ RvMedia::getImageUrl($image, null, false, RvMedia::getDefaultImage()) }}" class="showfullimg" rel="{{ $loop->index }}" alt="{{ $property->name }}"></video></div>
        @endif
    </div>
@endforeach

它检查你的 $property->name,如果它有 '.jpeg' 字符串然后显示 img 标签。否则,显示视频标签。

【讨论】:

    猜你喜欢
    • 2018-12-16
    • 2014-10-30
    • 2020-12-26
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 2016-06-01
    相关资源
    最近更新 更多