2017-04-26 108 views
1

我有2個模型是多對多的關聯。Octobercms:多對多關係沒有顯示

車間

public $belongsToMany = [ 
    'registrations' => [ 
    'Wooltown\Events\Models\Registration', 
     'table' => 'wooltown_events_registration_workshop', 
     'count' => true, 
    ], 
]; 

註冊

public $belongsToMany = [ 
    'workshops' => [ 
    'Wooltown\Events\Models\Workshop', 
     'table' => 'wooltown_events_registration_workshop', 
     'count' => true, 
    ], 
]; 

我取的記錄是這樣的:

$vars['registration'] = Registration::with('workshops')->find($registration->id); 

我期待的所有相關車間的數組(在這個CAS裏Ë2)中的結果,但該作坊陣列只顯示我:

... 
"workshops": [{ 
    "registration_id": 113, 
    "count": 2, 
    "pivot": { 
     "registration_id": 113 
    } 
}] 
... 

缺少什麼我在這裏?我如何獲得相關車間模型的集合?

回答

2

您應該使用計數器作爲一個獨立的關係:

public $belongsToMany = [ 
    'workshops' => [ 
     'Wooltown\Events\Models\Workshop', 
     'table' => 'wooltown_events_registration_workshop', 
    ], 
    'workshops_count' => [ 
     'Wooltown\Events\Models\Workshop', 
     'table' => 'wooltown_events_registration_workshop', 
     'count' => true, 
    ], 
]; 
+0

是的,定了!謝謝@dragontree – Abel