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

dillscher's avatar

Check a single item within a query scope

Hi there,

I am using a scopeRegion function to filter on items belonging to the current localization region like de, gb, us, ....

The scope looks like this:

public function scopeRegion($query, $locale = null)
    {
        $locale = ($locale) ?? LaravelLocalization::getCurrentLocale();

        $q = clone $query;

        $localeRegion = $q->where('regions->' . $locale, true);

        if ($localeRegion->count() > 0) {
            return $query->where('regions->' . $locale, true);
        }

        //  Check, if assigned to any other region

    //  Exclude items from query where another region like 'regions->gb' === true

        //  only fallback, if not restricted to another region
        return $query->where('lang', getParentLangByRegion($locale));

    }

Is there any way I could exclude items from query if the are assigned to another region like the current one? Only if the are not assigned to any region I would like to pass the fallback.

Cheers

Chris

0 likes
2 replies
Borisu's avatar

Do you really have a field with an arrow symbol in the column name? Think about it...

If I understand you have a table with a regions column, which is supposedly a relationship. So just get the objects where the relationship returns an empty object.

dillscher's avatar

I just use a regions column which is a json field. Therefore the syntax with an arrow.

Please or to participate in this conversation.