dongzong2017 2018-07-16 11:39
浏览 96

在laravel中,如何从一个表中检索数据时排除行,这些表链接到另一个表上的已删除行

I have a model like this

public function getProduct() {

        return $this->belongsTo('App\Product','product_id','id');

    }

This function is from a model called Support. The data in product_id matches to the id column in Product table. Some of the entries in supports table fetch product details using this relationship function. Now, when a row in the products table, for example:having id = 4, gets deleted or softdeleted, the rows in the supports table having product_id = 4 gets affected i.e. for a code like this

{{$support->getProduct->name}}

gives an error saying "Trying to get the property of a non object".

How do i get around this error such that if the rows in product table gets deleted or soft deleted, the rows in supports table having product_id equal to the id in products table do not get retrieved during query?

  • 写回答

1条回答 默认 最新

  • dongshi6844 2018-07-16 12:31
    关注

    if you can do this using blade.

    {{$support->getProduct->name or ''}}
    

    or

    {{ isset($support->getProduct->name) ?  $support->getProduct->name:'' }}
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度