Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

421
Views
How to do this in laravel, Subquery where count

I am trying to implement this query in Laravel

"SELECT * FROM jobs 
WHERE status = 1 AND 
taskerCount = (SELECT count(*) FROM jobRequests WHERE status = 1 AND job_id = 
jobs.id)"

This is what i have tried;

Auth::user()->jobs
        ->where('status', 1)
        ->where('taskerCount', function ($q) {
            $q->where('status', 1)
              ->where('job_id', $q->id)->count();
        });

But I get the error:

Object of class Closure could not be converted to int.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use a raw expression instead of -count().

You can view this documentation here

Example of a raw query from laravel:

$users = DB::table('users')
                 ->select(DB::raw('count(*) as user_count, status'))
                 ->where('status', '<>', 1)
                 ->groupBy('status')
                 ->get();

Make sure you define the count as a name.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!