Filter and count related model data

Asked

Viewed 26 times

0

Enterprise hasMany Setorempresa

Setorempresa belongsTo Enterprise

Setorempresa hasMany Functionary

Functionary belongsTo Setorempresa

With this data in hand, how do I count how many id_status = 1 employees are allocated in sectors of type = "Administrative"? I tried it the way down, but it didn’t work and I guess I’m away the right way.

{{ $empresa->SetorEmpresa->where('tipo', 'Administrativo')->Funcionario->where('id_status', 1)->count() }}

I am using Laravel 5.1 and this data will be loaded into a view from the Company object.

  • You need to be clearer, your question has become very confusing. Try to give more details, so we can help you

  • thanks, but I managed to solve @Wallacemaxters

1 answer

0


Well, I got it this way:

{{ $adm = 0 }}

@foreach($empresa->SetorEmpresa->where('tipo', 'Administrativo') as $setor_adm)
     {{ $adm += $setor_adm->Funcionario->where('id_status', 1)->count() }}
@endforeach

{{ $adm }}

So it goes through all the company’s administrative sectors and counts how many active employees it has in each of them

Browser other questions tagged

You are not signed in. Login or sign up in order to post.