php - Laravel 5.3 relationship -
here code:
class company extends model { public function employees() { return $this->hasmany('employee'); } } class employee extends model{ protected $table = "user_role_company"; public function user(){ return $this->belongsto('user'); } public function company(){ return $this->belongsto('company'); } } class user extends authenticatable { public function employee(){ return $this->hasmany('employee'); } }
if run:
$recordstotal = user::with(['employee' => function ($query) use ($company_id) { $query->where('company_id', $company_id); }])->count();
it returns users count not empolyee count.
what doing wrong?
thanks
first of all, have 'company_id' , 'user_id' column in employee model/db?
anyways, can try query instead.
$recordstotal = user::wherehas('employee', function ($q) use ($company_id) { $q->where('company_id', $company_id); })->count();
Comments
Post a Comment