Support the ongoing development of Laravel.io →
posted 4 years ago
Laravel

I use global scope based on the current auth but getting error: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous

Is it possible to get the table name for the current model inside of addGlobalScope? E.g Without $this needed for qualifyColumn.

` protected static function boot() { parent::boot();

    if (!auth()->user() || !auth()->user()->isManagement()) {
        static::addGlobalScope('valid', function (Builder $builder) {
            $builder->where($this->qualifyColumn('status'), 1);
        });
    }
}

`

This just gets messy having to pass the name

` protected static function boot() { parent::boot();

$table_name = 'employment_contracts';

if (!auth()->user() || !auth()->user()->isManagement()) {
    static::addGlobalScope('valid', function (Builder $builder) use($table_name) {
        $builder->where("{$table_name}.status", '=', AbecederSystem::$ENABLED);

        $builder->where(function ($q) use($table_name) {
            $q->where("{$table_name}.valid_from", '<=', Carbon::now());

            $q->where(function () use ($q, $table_name) {
                $q->where("{$table_name}.valid_until", '>=', Carbon::now())
                    ->orWhereNull("{$table_name}.valid_until");
            });
        });
    });
}

} `

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Gizmo gizmo Joined 16 Mar 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.