从集合中删除(如果为空)

时间:2019-12-30 19:09:28

标签: php laravel

products []为空时如何删除项目?

这是我从数据库中获取它的方式:

mainDocPart.Document = doc; //you define earlier but do not used.
doc.Body = new Body();
SectionProperties sectionProperties1 = 
mainDocPart.Document.Body.Descendants<SectionProperties>()?.FirstOrDefault();  
if (sectionProperties1 == null)
{
    sectionProperties1 = new SectionProperties() { };
    mainDocPart.Document.Body.Append(sectionProperties1);
}

这是结果:Result

但是,仅当products []不为空时,我才需要这些项目。我该怎么办?

例如此处:example 这些产品是空的,但是现在我要删除所有这些数据。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用with进行回调并使用whereHas进行相同的回调来过滤急切的负载,以删除不需要的order_details

尝试这样的事情:

$catIds = Categorie::where('place', $place)->pluck('id');

$orders = Order::with(['order_details' => function ($q) use ($catIds) {
    $q->whereHas(['products' => $func = function ($q) use ($catIds) {
        $q->whereIn('categorie_id', $catIds)
    })->with(['products' => $func]);
}])->get();

否则,您可以遍历集合并删除不需要的记录。