This is my last attempt to create a complex query in Laravel. I have 3 tables I need for this scenario: photos, events, countries
. Each event has many or none photos, each country may have multiple events with photos.
My result shows
PhotosByCountry, EventsByCountry, rContinent, rCountry.
And here is the well functioning MySQL native query:
SELECT SUM( allPhotos ) AS PhotosByCountry,
COUNT( temp.Land ) AS EventsByCountry,
rContinent,
temp.Land AS rCountry
FROM (
SELECT e.country AS Land, COUNT( p.id ) AS allPhotos, c.continent AS rContinent
FROM `photos` p
INNER JOIN `events` e ON e.id = p.eventID
INNER JOIN countries c ON e.country = c.country
GROUP BY p.eventID
)temp
GROUP BY rCountry
Who can help me translating this into Laravel Query builder without DB::raw()
or whereRaw()
. My main issue in building that thing is the sub query.
I got Models
for all tables: Photo, Country, Sportevent (for table events (= legacy name), couldn't use Event)
.
Thanks for your efforts, I'm happy to provide additional info if needed.
Addition
Tables:
events
id | name | country ... has more columns of course
1 | Eventname 1 | France
2 | Eventname 2 | Switzerland
3 | Eventname 3 | France
photos
id | eventID | path ...
1 | 2 | .....
2 | 1 | .....
3 | 2 | .....
4 | 3 | .....
5 | 3 | .....
6 | 2 | .....
countries
id | country | continent (or geographical Region) ...
1 | France | Europe
2 | Switzerland | Europe
3 | Germany | Europe
4 | United States| North America
5 | Australia | Oceania
6 | .....
Result
PhotosByCountry | EventsByCountry | rContinent | rCountry
3 | 2 | Europe | France
3 | 1 | Europe | Switzerland