gwapo
gwapo

Reputation: 208

Calling a method inside another method and passing a parameter in a controller

The first method has a condition of the query and it returns the count of the query specified.The 2nd method calls the 1st method. First question, how can i pass the parameter $compid to Method countEmployee from Method userSummary? Second question, Is this the right thing to call method countEmployee inside method userSummary?

1st Method

public function countEmployee($compid){
                    $query = User::query();
                    $company = $query;
                    // $thgarments = $query;
                    $company->whereHas('company', function ($q) {
                        $q->where('company_id','=', $compid);
                    });
                    $company->where('emp_status','=', 'Regular');
                    $totalemployee = $company->count();

                    return $totalemployee;
    }

2nd Method

public function userSummary()
{
     $tenghwa = countEmployee(2);
}

Upvotes: 0

Views: 1258

Answers (2)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

Try this corrected code

public function countEmployee($compid) 
{
     $query = User::query();

     $totalemployee = $query->whereHas('company', function ($q) use ($compid) {
         $q->where('company_id','=', $compid);
     })->where('emp_status','=', 'Regular')
     ->count();

    return $totalemployee;
}

Upvotes: 1

gwapo
gwapo

Reputation: 208

public function countEmployee($compid){
                    $query = User::query();
                    $company = $query;
                    // $thgarments = $query;
                    $company->whereHas('company', function ($q) use($compid) {
                        $q->where('company_id','=', $compid);
                    });
                    $company->where('emp_status','=', 'Regular');
                    $totalemployee = $company->count();

                    return $totalemployee;
    }

    public function userSummary()
    {
         $tenghwa = $this->countEmployee(2);
    }

I realized my mistake. I should "use" $compid in the first method, and in the second method i should put a $this to calling the countEmployee method.

Upvotes: 0

Related Questions