【发布时间】:2018-06-25 00:02:10
【问题描述】:
我想在视图中有一个表格,显示与会议相关的所有注册类型、每种注册类型的价格以及每种注册类型的售出注册数量。
例如,如果 id 为“1”的会议有两种注册类型,其中包含这些列和值:
- rtype1(名称),0.00$(价格),20(容量)
- rtype2(名称),5.00$(价格),10(容量)
并且在 rtype1 中注册了 20 个参与者,在 rtype 2 中注册了 5 个参与者我想显示一个表格,如下所示:
Registration Type name Price Sold/capacity
rtype1 0.00$ 20/20
rtype2 5.00$ 5/10
我不明白如何正确地实现这一点。现在我有这个查询来获取每个注册类型和每个注册类型的计数:
$participantsOfEachType = Participant::
select(DB::raw('registration_type_id, count(registration_type_id)'))
->groupBy('registration_type_id')->get();
此查询返回以下结果。但不考虑具体的会议。您是否知道考虑特定会议 ID 需要什么,以便可以在表格中显示结果,如上表示例中的那样?
Collection {#271 ▼
#items: array:2 [▼
0 => Participant {#282 ▼
...
#attributes: array:2 [▼
"registration_type_id" => 1
"count(registration_type_id)" => 2
]
...
}
1 => Participant {#279 ▼
...
#attributes: array:2 [▼
"registration_type_id" => 2
"count(registration_type_id)" => 2
]
...
}
]
}
问题的相关模型:
用户模型:
public function registrations(){
return $this->hasMany('App\Registration','user_that_did_registration');
}
注册模式:
// user that did the registration
public function customer(){
return $this->belongsTo(User::class, 'user_that_did_registration', 'id');
}
public function participants(){
return $this->hasMany('App\Participant');
}
public function conference(){
return $this->belongsTo('App\Conference');
}
会议模型:
public function registrations(){
return $this->hasMany('App\Registration', 'conference_id');
}
参与者模式:
public function registration(){
return $this->belongsTo('App\Registration');
}
public function registration_type(){
return $this->belongsTo('App\RegistrationType');
}
【问题讨论】:
-
您能否包含您的模型以及它们之间定义的关系,或者为您的表提供表定义和示例数据集
-
谢谢我用模型更新问题。