skip to Main Content

I need a querybuilder query that calculates how many different people the user is messaging without counting the repetitive data.

database photo

Example:

conver_user_id = 165, conver_user_seller_id = 156
conver_user_id = 165, conver_user_seller_id = 156
conver_user_id = 165, conver_user_seller_id = 156
conver_user_id = 165, conver_user_seller_id = 158
conver_user_id = 165, conver_user_seller_id = 158

the result i want : Total count: 2

I will be glad if you help me thank you

2

Answers


  1. Assuming you have the user when the query is executed, as you ask "the user is messaging". Using group by, to select the unique conversations, then counting it.

    $uniqueSellersMessaging = Conversation::where('conver_user_id', $user->id)
        ->groupBy('conver_user_seller_id')
        ->count();
    
    Login or Signup to reply.
  2. You can use the distinct method on the query builder to achieve the desired result.

    $total_count = DB::table('conversations')
        ->where('conver_user_id', $user_id)
        ->select('conver_user_seller_id')
        ->distinct()
        ->count();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search