Laravel Count from database -
i want count amount of workers company has. shows like: mcdonalds - 200 employees
(example)
model:
public function gettotalworkers() { return $this->hasmany('app\user')->wherecompanyid($this->company_id)->count(); }
view:
@foreach($companies $company) <tr> <td>{!! link_to_route('company.edit', $company->name, [$company->id] )!!}</td> <td>{{ $gettotalworkers }}</td> <td>{{ ' /' }}</td> <td>{{ $company->parent_id }}</td> <td>{{ ' /' }}</td> <td>{{ $company->active }}</td> </tr> @endforeach
controller:
public function index() { $gettotalworkers = $this->company->gettotalworkers(); $companies = company::get(); return view('company.index', ['companies' => $companies, 'gettotalworkers' => $gettotalworkers]);
but gives me error: call member function gettotalworkers() on non-object
how can fix or doing wrong?
it looks calling gettotalworkers()
on $post
, not $company
on fourth line of view snippet have posted.
Comments
Post a Comment