首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何以雄辩的口才实现此查询,以便在表中显示结果?

如何以雄辩的口才实现此查询,以便在表中显示结果?
EN

Stack Overflow用户
提问于 2018-06-26 13:26:43
回答 1查看 34关注 0票数 0

我想展示一些信息,在一个会议的最后一次登记在一个表格中,如下所示。例如,本表显示最后一次登记是由John完成的,是有2名参与者的登记,注册的总价值为10.00。

代码语言:javascript
运行
复制
User that did the registration | Quantity of participants registered | Total Value
            John                               2                        10.00$
            Jake                               1                        5.00$           

我有一个查询,它显示了上面的结果:

代码语言:javascript
运行
复制
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

但是,我想用雄辩的语言来进行查询,这样就可以在表中的前景中显示结果:

代码语言:javascript
运行
复制
<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>

以雄辩的口才,我喜欢下面,但它不起作用。您知道如何在表中显示必要的数据吗?

代码语言:javascript
运行
复制
$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();

问题的相关模型:

用户模型:

代码语言:javascript
运行
复制
public function registrations(){
    return $this->hasMany('App\Registration','user_that_did_registration');
}

登记模式:

代码语言:javascript
运行
复制
// 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');
}

会议模式:

代码语言:javascript
运行
复制
 public function registrations(){
    return $this->hasMany('App\Registration', 'conference_id');
}

参加者模式:

代码语言:javascript
运行
复制
public function registration(){
    return $this->belongsTo('App\Registration');
}

public function registration_type(){
    return $this->belongsTo('App\RegistrationType');
}

登记类型模型:

代码语言:javascript
运行
复制
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');
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-26 14:02:24

尝尝这个

代码语言:javascript
运行
复制
$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();

像这样循环

代码语言:javascript
运行
复制
foreach($registrations as $registration){
    echo $registration->userName." - ".$registration->participants_count." - ". $registration->totalPrice;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51044074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档