我想在下表中显示有关会议中最近注册的一些信息。例如,此表显示,最后一次注册是由John完成的,并且是2位参与者的注册,注册总价值为10.00。
User that did the registration | Quantity of participants registered | Total Value
John 2 10.00$
Jake 1 5.00$
我有显示上述结果的查询:
SELECT users.name,COUNT(participants.registration_id),SUM(registration_types.price)
FROM participants
INNER JOIN registration_types
on participants.registration_type_id =
registration_types.id
INNER JOIN registrations
on registrations.registration_id =
participants.registration_id
INNER JOIN users
on users.id = registrations.user_that_did_registration
group by users.name
但是我想使用Eloquent进行查询,因此可以在表格的前额中显示结果:
<table class="table table-striped table-responsive-sm box-shaddow">
<thead>
<tr>
<th scope="col">User that did registration</th>
<th scope="col">Quantity</th>
<th scope="col">Date</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
...
</tr>
</tbody>
</table>
对于Eloquent,我喜欢以下内容,但它无法正常工作。您知道查询应该如何在表中显示必要的数据吗?
$registrationsInfo = Participant::
leftJoin('registration_types', 'participants.registration_type_id', '=', 'registration_types.id')
->leftJoin('registrations', 'registration.id', '=', 'participants.registration_id')
->leftJoin('users', 'users.id', '=', 'registrations.user_that_did_registration')
->selectRaw('users.name, count(participants.registration_id), sum(registration_types.price)')
->groupBy('users.name')
->get();
该问题的相关模型:
用户模型:
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');
}
注册类型模型:
class RegistrationType extends Model
{
public function conference(){
return $this->belongsTo('App\Conference');
}
public function participants(){
return $this->hasMany('App\Participant');
}
public function registrations(){
return $this->belongsToMany('App\Registration', 'registration_registration_types');
}
}
答案 0 :(得分:0)
尝试
$registrations = DB::table('registrations')
->join('participants', 'registrations.id', '=', 'participants.registration_id')
->join('users', 'users.id', '=', 'registrations. user_that_did_registration_id')
->join('registration_types', 'registration_types.id', '=', 'participants.registration_type_id')
->select( DB::raw('count(participants.registration_id) as participants_count, sum(registration_types.price) as totalPrice, users.name as userName'))
->groupBy('users.name')->get();
像这样圈起来
foreach($registrations as $registration){
echo $registration->userName." - ".$registration->participants_count." - ". $registration->totalPrice;
}