dquoj04882 2018-11-25 05:32
浏览 51

使用Laravel Eloquent上的连接计算来自不同表的行数

How can I use eloquent to count rows of another table joined with the main table?

I have three tables:

  • clients
  • sms notification
  • email notification

I want to join them by pulling the client list first, then count how many times they had notification from both SMS and email. Finally returning the count or 0 if no notification has been sent.

enter image description here

I found a sample: Laravel Eloquent to join table and count related

I used this for emails:

<?php

$clients = Client::where('cl_delete_status', false)
    ->leftJoin('insura_communication_emails', 'insura_clients.cl_id', 'insura_communication_emails.cl_id')
    ->leftJoin('insura_communication_sms', 'insura_clients.cl_id', 'insura_communication_sms.cl_id')
    ->select('*', DB::raw('count(ice_id) as total'))
    ->groupBy('insura_clients.cl_client_id')
    ->orderBy('insura_clients.cl_id', 'desc')->get();

How can I put both tables that are email and SMS insura_communication_sms and in the code above? It skips those with 0 counts in the email notification.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号