Write a query join sum groupby in laravel -
please me write query.
have 2 table: "projects" , "debts"
"debts" table have
id | project_id | currency_list | total 1 | 1 | 1 | 1000 2 | 1 | 2 | 500 3 | 2 | 1 | 1000 4 | 2 | 2 | 500 ...
i need write query take 1000 rows projects , sum "total" group "currency_list"
thanks lot :)
hey tried hope working :) first should have tow models tables in project model call
public function debts(){ return $this->hasmany('app\debt','project_id')->selectraw('debts.*,sum(total) sum')->groupby('currency_list'); }
and call in controller
$projects = project::with('debts')->get()->toarray();
check dd($projects) array
edit : use in controller function
$projects = db::table('projects') ->join('debts', 'projects.id', '=', 'debts.projects_id') ->select('projects.*','debts.*', db::raw('sum(total) sum')) ->groupby('currency_list') ->get();
then in view use
@foreach($projects $project) { {{$project->sum}} }
Comments
Post a Comment