mardi 26 janvier 2016

Eloquent eager load, select count and sum

My goal is to retrieve 'events' and 'eventtypes', count the number of 'events' on each day (groupby days) and also get the total sum of 'eventtypes.price' on each of those days. I'm having a problem accessing the eventtype table.

Event table

id
event_start

Eventtype table

id
price

Query (this query works)

    $events = Event::with('eventtype')
    ->select(DB::raw('DATE(events.event_start) as date'), DB::raw('count(*) as eventcount'))
    ->where('events.event_start', '>=', $rangestart)
    ->groupBy('date')
    ->orderBy('date', 'ASC')
    ->get();

Query (this query return error Column not found: 1054 Unknown column 'eventtypes.price' in 'field list')

    $events = Event::with('eventtype')
    ->select(DB::raw('DATE(events.event_start) as date'), DB::raw('count(*) as eventcount'), DB::raw('sum(eventtypes.price) as sumtotal'))
    ->where('events.event_start', '>=', $rangestart)
    ->groupBy('date')
    ->orderBy('date', 'ASC')
    ->get();        

Not sure why would eventtypes.price be unknown, it's clearly defined in my Model, and I use it else where.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire