I have three tables
**donates**
id| invoice_id| amount
**donate_project**
id| donate_id| project_id| data_close|
**project**
id| title
I need to get Donates from Project only those which are group by (donate_id , project_id)
having (count(*)%2 != 0)
like my query below
My code in Project model is valid
public function donates(){
$id = $this->id; //id of project
return $query = DB::table('donates')
->select('donates.id', 'donates.invoice_id', DB::raw("amount"))
->join('donate_project', function($join) use $id {
$join->on('donate_project.donate_id', '=', 'donates.id')
->where('donate_project.project_id', '=', $id);
})
->groupBy('donate_project.project_id', 'donate_project.donate_id')
->havingRaw("count(*) % 2 != 0")
}
So I try to do like donates() and returns only Collection but I need to return (Illuminate\Database\Eloquent\Relations\Relation
)
public function donates()
{
return $this->belongsToMany(Donate::class)
->getBaseQuery()
->selectRaw('sum(donates.amount) as aggregate, donates.id')
->groupBy('donate_project.project_id', 'donates.id')
->havingRaw('count(*) % 2 != 0');
}
Is it possible? sorry for my English:)
via Mykhailo Basenko