Count number of rows from a different table using join on Laravel Eloquent












0















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.










share|improve this question

























  • Add another left join same way you have your first leftJoin, rather than count you might want to use SUM.

    – Prix
    Nov 25 '18 at 5:39













  • i can, but the count, how will i do that?

    – Emil Kitua
    Nov 25 '18 at 5:42











  • Prix can you show me how? i have added another left join to the code

    – Emil Kitua
    Nov 25 '18 at 5:44











  • if you can post some sample data db-fiddle.com sure.

    – Prix
    Nov 25 '18 at 8:50
















0















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.










share|improve this question

























  • Add another left join same way you have your first leftJoin, rather than count you might want to use SUM.

    – Prix
    Nov 25 '18 at 5:39













  • i can, but the count, how will i do that?

    – Emil Kitua
    Nov 25 '18 at 5:42











  • Prix can you show me how? i have added another left join to the code

    – Emil Kitua
    Nov 25 '18 at 5:44











  • if you can post some sample data db-fiddle.com sure.

    – Prix
    Nov 25 '18 at 8:50














0












0








0








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.










share|improve this question
















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.







php mysql laravel eloquent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 11:47









Karl Hill

3,06622243




3,06622243










asked Nov 25 '18 at 5:32









Emil KituaEmil Kitua

146




146













  • Add another left join same way you have your first leftJoin, rather than count you might want to use SUM.

    – Prix
    Nov 25 '18 at 5:39













  • i can, but the count, how will i do that?

    – Emil Kitua
    Nov 25 '18 at 5:42











  • Prix can you show me how? i have added another left join to the code

    – Emil Kitua
    Nov 25 '18 at 5:44











  • if you can post some sample data db-fiddle.com sure.

    – Prix
    Nov 25 '18 at 8:50



















  • Add another left join same way you have your first leftJoin, rather than count you might want to use SUM.

    – Prix
    Nov 25 '18 at 5:39













  • i can, but the count, how will i do that?

    – Emil Kitua
    Nov 25 '18 at 5:42











  • Prix can you show me how? i have added another left join to the code

    – Emil Kitua
    Nov 25 '18 at 5:44











  • if you can post some sample data db-fiddle.com sure.

    – Prix
    Nov 25 '18 at 8:50

















Add another left join same way you have your first leftJoin, rather than count you might want to use SUM.

– Prix
Nov 25 '18 at 5:39







Add another left join same way you have your first leftJoin, rather than count you might want to use SUM.

– Prix
Nov 25 '18 at 5:39















i can, but the count, how will i do that?

– Emil Kitua
Nov 25 '18 at 5:42





i can, but the count, how will i do that?

– Emil Kitua
Nov 25 '18 at 5:42













Prix can you show me how? i have added another left join to the code

– Emil Kitua
Nov 25 '18 at 5:44





Prix can you show me how? i have added another left join to the code

– Emil Kitua
Nov 25 '18 at 5:44













if you can post some sample data db-fiddle.com sure.

– Prix
Nov 25 '18 at 8:50





if you can post some sample data db-fiddle.com sure.

– Prix
Nov 25 '18 at 8:50












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464930%2fcount-number-of-rows-from-a-different-table-using-join-on-laravel-eloquent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464930%2fcount-number-of-rows-from-a-different-table-using-join-on-laravel-eloquent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen