仍在尝试Eloquents关系查询,我想知道如果结构是以下,我的查询结果中如何有转换表中的setup
字段
具有表campaign_networks_relationships
+----------------------------------------------------------------------------------+
ID | networks_id | campaigns_id | setup
+----------------------------------------------------------------------------------+
1 | 1 | 1 | {"username":"username","password":"password"}
+----------------------------------------------------------------------------------+
2 | 1 | 2 | {"username":"username","password":"password"}
+----------------------------------------------------------------------------------+
活动
+-------------------+
ID | campaign_name
+-------------------+
1 | some name
+-------------------+
2 | test
+-------------------+
网络
+-------------------+
ID | network_name
+-------------------+
1 | network1
+-------------------+
2 | network2
+-------------------+
比我的Camnpaigns型号
public function networks() {
return $this->belongsToMany('TdReportsNetworksNetworks', 'campaign_networks_relationhsips');
}
和来自控制器的呼叫
public function getNetworks($id = null) {
$campaign = TdReportsCampaignsCampaigns::with('networks')->where('id', $id)->first();
foreach ($campaign->networks as $network) {
var_dump($network->network); //I have this Field
var_dump($network->setup); // I do not habe this Field !!!!
}
}
public function networks() {
return $this->belongsToMany('TdReportsNetworksNetworks', 'campaign_networks_relationhsips')
->withPivot(['setup', .. any other field you might want .. ]);
}
// then in the foreach loop you have:
...
$network->pivot->setup;