dongtun1872 2017-09-26 11:22
浏览 50
已采纳

Laravel:忽略刀片模板中的对象

I have a question concerning best practise with Laravel. I have a blade page who display all my products and I test if my product has image in my loop, I ignore it if it's the case :

       @foreach ($products as $product)
            @if(is_null($product->getMedia()->first())) 
                @continue
            @endif 
            <div class="col-md-4">
                 //display image and product informations
            </div>
        @endforeach

My question is : Is this correct ?

Or souhld I ignore the product who has no image in my controller ?

Thanks !

  • 写回答

2条回答 默认 最新

  • dounieyan2036 2017-09-26 11:43
    关注

    It depends whether you consider products without any images to be "invalid" / "inactive", or if you just don't want to show them in this particular view.

    Your controller is defining a business requirement like "fetch all products which match certain conditions". If "has an image" is part of that business requirement, because any product without an image should not be on sale, then the logic goes in your controller.

    Your view is defining a particular presentation like "arrange the products in a pretty grid". If "has an image" is just a requirement to work in this layout, skipping here could be reasonable.

    However, completely skipping products in the view may have some undesirable consequences:

    • If these products should be on sale, as implied by the controller returning them, hiding them means customers can't see them.
    • If you have pagination, or a total number of results found, it won't match the number of products the user can actually see unless you take care to handle the skipped products.

    It's therefore most appropriate if you are building some kind of "highlights", where the skipped products will actually be shown in a list further down, just not as prominently.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误