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

Reprise's avatar

Need a little help with a query involving withCount

Doing a search query right now and I am trying to get the count of the hires of the profile that owns the project.

        $query = Project::with('profile');
                    $query->with(['profile' => function ($q) {
                        $q->withCount('hires', function ($qr) {
                            $qr->where('hires_count', '=', '0');
                        });

the hires function belonging to the profile

    public function hires()
    {
        return $this->projects()->where('status', '<>', 'available')->whereHas('bids');
    }

and I am getting this error:

"mb_strpos() expects parameter 1 to be string, object given"

0 likes
3 replies
tykus's avatar

I have read this a couple of times and still don't understand what you are attempting to achieve; why are you eager-loading profile twice? The whereHas method takes a Closure; did you mean to use the has method instead?

Reprise's avatar

tried eager loading it second time because it wasn't letting me use it. And for the whereHas I was not aware that it takes a Closure, I simply wanted to return the projects that have the relationship 'bids'.

staudenmeir's avatar

withCount expects an array:

$query->with(['profile' => function ($q) {
    $q->withCount(['hires' => function ($qr) {
        $qr->where('hires_count', '=', '0');
    }]);

Please or to participate in this conversation.