Well, I have the following problem when using Laravel 5 in the table relationship:
I have these 4 tables (hypothetical names to illustrate the problem):
UsingtheLaravel5templatesI'velistedthemtogether:
CompanyTemplate
publicfunctionpessoa(){return$this->hasMany('App\Pessoas')}
PeopleTemplate
publicfunctiontrabalho(){return$this->hasMany('App\Trabalho')}publicfunctionempresa(){return$this->belongsTo('App\Empresa')}
WorkTemplate
publicfunctioncusto(){return$this->hasMany('App\Custo')}publicfunctionpessoa(){return$this->belongsTo('App\Pessoa')}
CostModel
publicfunctiontrabalho(){return$this->belongsTo('App\Trabalho')}
Myquestionishowtogetthesumofthecostofallthepeopleofacertaincompanyandofallthejobs...Forexample,toknowthetotalcostofajobIdosomethinglikethis:
$trabalho=newTrabalho->find($id)$custoDoTrabalho=$trabalho->custo->sum('montante')
However,ifyoudothefollowing:
$pessoas=newEmpresa->find($id)->pessoa$custoDosTrabalhosTodasAsPessoas=$pessoas->trabalho->custo->sum('montante')
Doesnotwork!
ToachievethedesiredresultIhavetowritemanylines(andI'mlazyandtheprojectisbigandwithmanycaseslikethese!)
$custo=0;$pessoas=newPessoa->find($id);$trabalhos=$pessoas->trabalho;foreach($trabalhosas$trabalho)$custo+=$trabalho->custo->sum('montante')
Isthereanywaytouse"Eloquent" relationships for this purpose, or do I really have to type everything?
(sorry for the large text ... thank you in advance!)