首页 > 解决方案 > 如何检查 Laravel 查询中哪个条件有数据

问题描述

type == 'supplier'如果没有,我希望找回type == 'retailer'。而我只希望检索type == 'retailer',如果两者type也有客户的话。

有没有办法将以下查询条件组合成一个查询?

$data = Sale::where('type', 'retailer')->where('customer', 'LIKE', '%peter%')->get();
if (count($data) == 0) {
   $data = Sale::where('type', 'supplier')->where('customer', 'LIKE', '%peter%')->get();
}

标签: laraveleloquent

解决方案


推荐阅读