Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bipin's avatar
Level 2

count and get in same query

hello guys i have 2 different table one is job_postings and second one is mi_rec_applicants here i just want to count value of mi_rec_applicants and hole data of job_postings in single query like this

                  0 => {#354 ▼
                              +"id": 12
                              +"userid": "[email protected]"
                              +"cmpnyname": "ABC Ltd"
                              +"job_pos_id": "JOB00000000000010014"
                              +"pos_stat": "Live"
                              +"intjobpos_id": "JOB00000000000010014"
                              +"job_title": "Beauty/Fitness/Spa Services"
                              +"job_type": "Permanent"
                              +"job_loc": "Mumbai"
                              +"no_of_pos": "12"
                              +"gender": "No_preference"
                              +"pref_nat": "Indian"
                              +"min_qual": "BOM"
                              +"pref_qual": "MOR"
 
                           }

here below +"pref_qual": "MOR" i want count value of table one how i can achieve this

0 likes
7 replies
tisuchi's avatar

You may try this-

DB::table('tablename')
            ->select(DB::raw('count(pref_qual) as total'), function($q){
                $q->where('pref_qual', 'MOR');
            })
            ->get();
bipin's avatar
Level 2

@tisuchi i had try this but not working

     $users = DB::table('job_postings')
                         ->select(DB::raw("jobpostingid FROM mi_rec_applicants 
                            WHERE jobpostingid ='$a' AND   accept='1'"))
                         ->join('mi_rec_applicants', 'job_postings.job_pos_id', '=', 
                             'mi_rec_applicants.jobpostingid')
                         ->get();
tisuchi's avatar

Well...

In this case, you need to write your query may be like this way-

$users = DB::table('job_postings')
                ->join('mi_rec_applications', 'job_posting.job_pos_id', '=', 'mi_rec_applications.jobpostingid')
                ->select(DB::raw('count(mi_rec_applications.pref_qual) as total'), function($q){
                    $q->where('mi_rec_applications.pref_qual', 'MOR');
                })
                ->get();


//I'm not sure that, mi_rec_applications comes from which table. If it is from different table, change in clouser 
bipin's avatar
Level 2

@tisuchi it throw error like this strtolower() expects parameter 1 to be string, array given

Please or to participate in this conversation.