#Possible to do counts on attributes? Querybuilder

9 messages · Page 1 of 1 (latest)

fiery adder
#

Wanting to ask if its possible to use existing attributes in a model and use them for counts.

My repository is based off a Lead model and this model contains 2 attributes i want to count.

    /**
     * Checks to see if the lead is cold (no action for a few days)
     * 
     * @return bool
     */
    public function getColdLeadAttribute():bool
    {
        return $this->lead_last_action >= 2 && ($this->status == Status::INITIATION->value || $this->status == Status::QUALIFICATION->value || $this->status == Status::DEAL_BUILDING->value);
    }

    /**
     * Checks to see if the lead is warm (close to closing a deal)
     * 
     * @return bool
     */
    public function getWarmLeadAttribute():bool
    {
        if($this->offerApprovedApprovals()->exists()){
            return $this->status == Status::DEAL_BUILDING->value && $this->offer_approved_approvals_count;
        }else {
            return false;
        }
    }
stray sonnet
#

It's not really clear from this code what you want to count

fiery adder
#

@stray sonnet i would like to call this someone as a re-use of code to count the warm leads and cold leads.

#
    public function getLeadStats(array $filters): Lead
    {
        @['search' => $search] = $filters;

        $queryBuilder = $this->model;

        $queryBuilder->withCount('warm_leads');
stray sonnet
#

Once you create a local scope, you can do the following:

YourModel::warmLead()->count();
fiery adder
#

thank you

stray sonnet
#

You're very welcome 🙂

trail wren