2016-07-29 27 views
0

我正在使用Laravel 5.2,并且在我的模型设置之间存在一些关系。如何仅在关系尚未脱机的情况下返回与其关系的记录

我有一个Product模型和一个Customer模型(产品有一个客户)。 我正在使用以下来获取客户列表,但我也使用软删除。如果客户被软删除(关系),我不想退货。

如何在Laravel中实现此目标?

$products = Product::with('customer')->get(); --want说 “哪里customer.deleted_at为空”

回答

1

你必须调用has上查询:

$products = Product::with('customer')->has('customer')->get();

1

由于@Lock所说的那样,使用

$products = Product::with('customer')->has('customer')->get(); 

提示:相反 - 即只得到删除客户的产品(不会产生太多的se在这里,但你可能会发现这在以后很方便),使用下面

$deleted=Product::with(['customer' => function ($q) { 
    $q->withTrashed(); 
    }])->onlyTrashed()->get(); 
相关问题