lundi 28 janvier 2019

Use pivot table to fetch data from main table

I am using 2 tables and a pivot table

Table 1 named calendars.

Table 2 named calendar_groups.

Pivot table calendar_calendar_group.

I am trying to get data from Table 1 based on a where value in the pivot table. So column where calendar_groups_id = 1 than use calendar_id to get data from table 1.

I can not get it to work.

Code i got so far:

$event = new Calendar();

    $event ->orderBy('start', 'asc')
            ->whereHas('calendar_groups', function($q) {
                $q->wherePivot('calendar_groups_id','=', '1');
            })->with('calendar_groups')
            ->first()
    ;

This gives me the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in 'where clause'

Your help is very much appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire