简体   繁体   中英

Laravel - How to Resolve Object of class stdClass could not be converted to string Error

When I run this code:

   public function manager_employee_list()
   {
       $userCompany = Auth::user()->company_id;
       $userEmployee = Auth::user()->employee_id;
       $identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->first();

       $linemanager = DB::table('hr_employees')->select('line_manager_id')->where('id', $userEmployee)->first();
       $linemanageremployee = DB::table('hr_employees')->select('id')->where('line_manager_id', $linemanager->line_manager_id)->get();  
       $goals = AppraisalGoal::where('employee_id', $linemanageremployee)->where('appraisal_identity_id', 
       return view('appraisal.appraisal_goals.manager_employee_list')->with('goals', $goals);        
}

I got this error:

Object of class stdClass could not be converted to string

When I did:

dd($linemanageremployee);

I got:

Illuminate\Support\Collection {#880 ▼
  #items: array:3 [▼
    0 => {#2312 ▼
  +"id": 2
}
1 => {#2326 ▼
  +"id": 3
}
2 => {#2313 ▼
  +"id": 6
    }
  ]
}

How do I resolve it?

Thank you.

Try changing:

$linemanageremployee = DB::table('hr_employees')->select('id')->where('line_manager_id', $linemanager->line_manager_id)->get();

to

$linemanageremployee = DB::table('hr_employees')->select('id')->where('line_manager_id', $linemanager->line_manager_id)->pluck('id');

Then change:

$goals = AppraisalGoal::where('employee_id', $linemanageremployee) ...

to

$goals = AppraisalGoal::whereIn('employee_id', $linemanageremployee) ...

Explanation:

->get() method returns an Eloquent Collection, which cannot be directly used inside of where() to make a query.

->pluck() method returns an array, which can be used to make a query with whereIn()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM