Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query building is failing if using '->having()' method #51533

Closed
Indianos opened this issue May 22, 2024 · 2 comments
Closed

Query building is failing if using '->having()' method #51533

Indianos opened this issue May 22, 2024 · 2 comments

Comments

@Indianos
Copy link

Laravel Version

10.48.11

PHP Version

8.2.17

Database Driver & Version

No response

Description

I am trying to migrate an old code to laravel and it's using SQL 'HAVING' clause. However if I try to compose via Query/Builder::having(Clousure) it throws an error:

TypeError

count(): Argument #1 ($value) must be of type Countable|array, null given

at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2298
2294▕ * @return $this
2295▕ */
2296▕ public function addNestedHavingQuery($query, $boolean = 'and')
2297▕ {
➜ 2298▕ if (count($query->havings)) {
2299▕ $type = 'Nested';
2300▕
2301▕ $this->havings[] = compact('type', 'query', 'boolean');
2302▕

Steps To Reproduce

\Illuminate\Support\Facades\DB::query()
    ->select()
    ->from('some_table')
    ->having(fn ($query) => $query->where('id', '>', 0));
@staudenmeir
Copy link
Contributor

Hi @Indianos,
You need to call having() inside the closure:

->having(fn ($query) => $query->having('id', '>', 0));
                                ^^^^^^

@crynobone
Copy link
Member

Thanks @staudenmeir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants