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

madsynn's avatar

Is there a shorter way to do this query?

Just curious if there are any other ways to get the same result for this query.

$categories = ProductCategory::whereHas('products', function($query) use($request) {
	$query->whereNotIn('type', ['service', 'misc']);
})->published()->get();

Or This query :

 $supports = Support::groupBy('support_status_id', '!=', 4 )->havingRaw('COUNT(*) > 1')->get();

Scope question? Is there a scope to return a count of all non closed status between support and support status tables.

scope_statuses >

1:pending
2:assigned
3:on hold
4:closed
0 likes
5 replies
MichalOravec's avatar

Yes it is

$categories = ProductCategory::whereHas('products', function ($query) {
	$query->whereNotIn('type', ['service', 'misc']);
})->published()->get();
1 like
madsynn's avatar

@michaloravec

Thank you, That's the only diff huh? Thank you. I should have noticed that myself but wasnt paying to much attention.

guybrush_threepwood's avatar

You could also create a scope to store the logic in your model:

// With hardcoded types
public function scopeContainsRealProducts($query)
{
    return $query->whereHas('products', function ($q) {
        $q->whereNotIn('type', ['service', 'misc']);
    });
}

$categories = ProductCategory::containsRealProducts()
    ->published()
    ->get();
// With types as a parameter
public function scopeContainsProducts($query, $type)
{
    return $query->whereHas('products', function ($q) use ($type) {
        $q->whereNotIn('type', $type);
    });
}

$categories = ProductCategory::containsProducts(['service', 'misc'])
    ->published()
    ->get();
madsynn's avatar

@guybrush_threepwood

I am trying to setup scopes for trying to retrieve my support tickets by support category but not quite getting it. could you tell me what I am missing please. I have never used scopes so I am trying to wrap my head around how they work.

Two Models

  • Support
  • SupportCategory

Support.php

	public function case_reason()
	{
		return $this->belongsTo(SupportCategory::class, 'case_reason_id');
	}

	public function support_status()
	{
		return $this->belongsTo(SupportStatus::class, 'support_status_id');
	}

SupportCategory.php

    public function case_reasons()
    {
        return $this->hasMany(Support::class, 'case_reason_id', 'id');
    }

Support Scopes Attempts: (not working)

	public function scopeIsReturn($query)
	{
		return $query->case_reason()->where('id', 1);
	}

	public function scopeIsTroubleshooting()
	{
		return $query->case_reason()->where('id', 2)->get();
	}
guybrush_threepwood's avatar
Level 33

Hi @madsynn

When working with scopes you are defining query constraints that are going to be applied on an Illuminate\Database\Eloquent\Builder instance ($query).

To be able to access model relationships you're expected to have an instance of the model, in your case App\Model\Support (which you don't when working with scopes).

On the other hand this should work:

	public function scopeIsReturn($query)
	{
		return $query->whereHas('case_reason', function ($q) {
            $q->where('id', 1);
        });
	}

This will return all "Support" records that have a corresponding "SupportCategory" with an ID of 1.

Of course it would be much easier to just query the foreign key of the support table:

	public function scopeIsReturn($query)
	{
		return $query->where('case_reason_id', 1);
	}
1 like

Please or to participate in this conversation.