我想展示一些信息,在一个会议的最后一次登记在一个表格中,如下所示。例如,本表显示最后一次登记是由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但是,我想用雄辩的语言来进行查询,这样就可以在表中的前景中显示结果:
<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>以雄辩的口才,我喜欢下面,但它不起作用。您知道如何在表中显示必要的数据吗?
$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');
}
}发布于 2018-06-26 14:02:24
尝尝这个
$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;
}https://stackoverflow.com/questions/51044074
复制相似问题