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

OldEr's avatar
Level 2

Query fails on production but runs on local environment

Hello.

The next query in successfully running on development environment, but fails on production one:

\App\Company::withCount('activesales')->where('activesales_count', '>', 0)->get();

(Returning "Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'activesales_count' in 'where clause'" error).

Company.php:

public function activesales()
    {
        return $this->hasMany('App\Post')->activeSale();
    }

Post.php:

public function scopeActiveSale($query)
    {
        return $query->where('type', 'discount')->where('started_at', '<', Carbon::now())->where('finished_at', '>', Carbon::now());
    }

Does anybody know what's the reason?

Thanks, in advance.

0 likes
9 replies
Bercove's avatar

Do you have that column activesales_count in your table company

OldEr's avatar
Level 2

No, this attribute is generated by withCount method.

jlrdw's avatar

Many times this is because Linux is case-sensitive so check out your letter sizes.

1 like
OldEr's avatar
Level 2

I've checked it several times. Everything is looking fine. "activesales_count" attribute is present at company-collection, but when I try to use it in where clause it fails.

jlrdw's avatar

activesales_count or activesales_Count

Again Linux is case-sensitive.

Check your name spacing check your names of your classes check the names of the files Etc.

OldEr's avatar
Level 2

Solved a problem on production using "having" instead of "where":

\App\Company::withCount('activesales')->having('activesales_count', '>', 0)->get();

But on the development environment query above causes another error:

SQLSTATE[HY000]: General error: 1 a GROUP BY clause is required before HAVING
Cronix's avatar

try turning strict mode off in your db config for that box.

1 like
OldEr's avatar
Level 2

I'm using sqlite at development environment. Added 'strict' => false to my config/database.php file at 'sqlite' section. Run "php artisan config:clear" but the error still persists.

Thanks anyway.

Cronix's avatar

So you're using one db engine in production and a totally different one in dev? They're not totally 100% compatible. You will get different results occasionally with certain queries. Use the same db engine in dev and production.

Please or to participate in this conversation.