Really struggling with the query and look around I can't find the answer that I require!
I have 4 tables, offers, users, savedoffers and usedoffers. savedoffers/usedoffers tables are relational tables between users & offers.
I want to return a list of offer then left join usedoffers and savedoffers. Then group them by the offer (say offer id) and then count how many users have saved the offers and how many have used them.
After all my efforts I am here...
$offers = DB::table('offers')
->select('offers.id as id',
'offers.code',
'offers.title',
'offers.date',
DB::raw('count(usedoffers.id) as used_count'),
DB::raw('count(savedoffers.id) as saved_count'))
->leftjoin('savedoffers', 'offers.id', '=', 'savedoffers.offer_id')
->leftjoin('usedoffers', 'offers.id', '=', 'usedoffers.offer_id')
->orderBy('offers.date', 'asc')
->groupBy('offers.id')
->get();
This still doesn't work as it seems to count both :/
Much help is appreciated as feel like im banging my head against a wall!