Laravel在连接外键时有何不为空然后在哪里应用

时间:2019-01-29 14:59:02

标签: laravel eloquent laravel-5.7

这是我使用出色的public class Daemon extends ParallelQuartzDaemon { @Override protected ICollectorAgent.ProcessStatus executeWork(JobExecutionContext jobExecutionContext, Map<String, Double> properties, Map<String, String> alerts) throws Exception { ICollectorAgent.ProcessStatus processStatus = ICollectorAgent.ProcessStatus.SUCCESS; List<FlowRequest> flowRequests = getFlowRequestsForTranslation(); if (!flowRequests.isEmpty()) { //DO Work! //takes 1-2 minutes! } return processStatus; } private List<FlowRequest> getFlowRequestsForTranslation() { translatorContext.getTranslatorJMSService().getFlowRequestsToTranslate(5); } } public class TranslatorJMSService extends JMSService { public List<FlowRequest> getFlowRequestsToTranslate(int count) { final Long jmsReceiveTimeoutMillis = translatorConfiguration.getJmsReceiveTimeoutMillis(); return (List<FlowRequest>) receive(count, queueName, jmsReceiveTimeoutMillis); } } 函数的查询。

SELECT CASE

WHEN trim(a.FA_CLS) = 'EQUIP'  AND a.FA_DEXA = '50020' AND a.FA_MEXA = '51000' THEN 'Pass'
WHEN trim(a.FA_CLS) = 'VEHADM'  AND a.FA_DEXA = '60220' AND a.FA_MEXA = '60330' THEN 'Pass'
WHEN trim(a.FA_CLS) = 'VEHSAL'  AND a.FA_DEXA = '60220' AND a.FA_MEXA = '60330' THEN 'Pass'
WHEN trim(a.FA_CLS) = 'VEHPRT'  AND a.FA_DEXA = '50040' AND a.FA_MEXA = '58000' THEN 'Pass'
WHEN trim(a.FA_CLS) = 'VEHSHO'  AND a.FA_DEXA = '50040' AND a.FA_MEXA = '58000' THEN 'Pass'
WHEN trim(a.FA_CLS) = 'VEHRNT'  AND a.FA_DEXA = '50040' AND a.FA_MEXA = '58000' THEN 'Pass'
WHEN trim(a.FA_CLS) = 'SHOP'  AND a.FA_DEXA = '50030' AND a.FA_MEXA = '51000' THEN 'Pass'
WHEN trim(a.FA_CLS) = 'FURN'  AND a.FA_DEXA = '60210' AND a.FA_MEXA = '61200' THEN 'Pass'
WHEN trim(a.FA_CLS) = 'LEASE'  AND a.FA_DEXA = '60200' AND a.FA_MEXA = '61200' THEN 'Pass'
WHEN trim(a.FA_CLS) = 'GOOD' AND a.FA_DEXA = '61000' AND a.FA_MEXA = '61200' THEN 'Pass' 
ELSE 'Fail' 
END 
FROM [QS36F.FASSET] a
WHERE a.FA_STA = 'A'
AND a.FA_DC != '90'

证书和站点与列表表具有多对一关系。

但是,直到列表表中的所有项目都为with,以上代码才能正常工作。但是有时whereHas可以是$list = $list->with([ 'site' => function($query) { $query->select('site_id', 'name')->where('status', 1); }, 'certification' => function($query) { $query->select('certification_id', 'name')->where('status', 1); }, ])->whereHas('site', function($query) { $query->where('status', 1); }) ->whereHas('certification', function($query) { $query->where('status', 1); }); ->where('status', '1')

如何编写此查询,以便仅在certification_id不为null时实现certification_id。以便给出所有结果certification_id +给出所有结果Null

列表中的关系为

whereHas

1 个答案:

答案 0 :(得分:1)

浪费了4个小时,在我发布此问题时点击了它。我将whereHascertification_id is null嵌套在where块中,并重新排列了!

$list = $list->with([
    'site' => function($query) {
        $query->select('site_id', 'name')->where('status', 1);
    },
    'certification' => function($query) {
        $query->select('certification_id', 'name')->where('status', 1);
    }
])->whereHas('site', function($query) {
    $query->where('status', 1);
})
->where(function($query) {
    $query->whereHas('certification', function($query) {
        $query->where('status', 1);
    })->orWhereNull('certification_id');
})