Spatie QueryBuilder/Eloquent – filter based on child attribute

One of those ‘all the time’ patterns I can never quite remember that is hard to google for.

The piece of code:

QueryBuilder::for(Entity::class)->whereHas('entityType', function($q) {
            $q->where("entity_types.is_task", true);

Explanation:

QueryBuilder::for(<Parent Class>)->whereHas('<relationship name of child/relative>', function($q) {
            $q->where("<table name>.is_task", true);

In this case, the child/relative is defined like this:

    public function entityType()
    {
        return $this->belongsTo(EntityType::class);
    }