简体   繁体   English

Laravel Eloquent 有条件

[英]Laravel Eloquent wherehas with condition

I have a gebruikers (meaning users) table.我有一个 gebruikers (意思是用户)表。 Gebruikers are grouped in a team (many to many relationship). Gebruikers 被分组在一个团队中(多对多关系)。 Gebruikers can have a breakfast, diner and supper. Gebruikers 可以享用早餐、晚餐和晚餐。 They eat together with their team colleagues.他们和团队同事一起吃饭。

Meals are registered in a mealregistration table.膳食登记在膳食登记表中。 So there is also a many to many relationship between the gebruikers table and the mealregistration table.所以gebruikers表和mealregistration表之间也存在多对多的关系。 A gebruiker can have many mealregistrations, a mealregistration has many gebruikers.一个gebruiker 可以有多个mealregistration,一个mealregistration 有多个gebruikers。

Not every team member takes every meal.不是每个团队成员都吃每一顿饭。 Some gebruikers only have breakfast, or dinner, or supper, or any combination of meals.一些 gebruikers 只吃早餐、晚餐、晚餐或任何餐点组合。

This is my code for saving a mealregistration这是我保存用餐登记的代码

$userid = Auth::user()->id;
    
    $nieuwemaaltijd = ModelsMaaltijdregistratie::create([
        'user_id' => $userid,
        'datum' => $this->datum,
        'afdeling_id' => $this->selectedAfdeling,
        
        'type' => $this->type,
        'prijs' => appsetting($this->datum,$this->type)
       
    ]);
    $nieuwemaaltijd->gebruikers()->sync($this->selectedGebruikers);

the user_id is the id of the teamleader making the registration, afdeling_id stands for the team, type is breakfast, diner or supper and price is the money charged for the meal (I made a function in a helper class for that). user_id 是进行注册的团队负责人的 id,afdeling_id 代表团队,类型是早餐、晚餐或晚餐,价格是餐费(我为此在助手 class 中制作了 function)。

I'm asked to provide a list of users that take more than one meal a day and how many days (in a certain period) they take more than one meal.我被要求提供一份每天多吃一顿饭的用户列表,以及他们吃多顿饭的天数(在一定时期内)。

So for example if gebruiker x takes breakfast and diner on Monday, only supper on Thursday and all three meals on Wednesday, he should be in the list with count 2 (since he took more than one meal on 2 days).因此,例如,如果 gebruiker x 周一吃早餐和晚餐,周四只吃晚餐,周三吃三顿饭,他应该在列表中,计数为 2(因为他在 2 天内吃了不止一顿饭)。

I'm getting close with:我越来越接近:

$gebruikers = Gebruiker::whereHas('maaltijdregistraties', function ($query) {
        $query->groupBy('datum')
        ->havingRaw('COUNT(*) > 1'); })->get();

It gives me a list with all the gebruikers that have at least one day on which the took more than one meal.它给了我一份清单,上面列出了所有至少有一天吃了不止一顿饭的 gebruikers。

But how can I display the number of days that they took more than one meal?但是我怎样才能显示他们吃不止一顿饭的天数呢?

Thanks!谢谢!

You may take a look at counting related models on the Laravel documentation.您可以查看 Laravel 文档中的计数相关模型

$gebruikers = Gebruiker::withCount(['maaltijdregistraties' => function (Builder $query) {
    $query->groupBy('datum')->havingRaw('COUNT(*) > 1');
}])->get();

echo $gebruikers[0]->maaltijdregistraties_count;

I succeeded in my goal by changing my code to the following:通过将代码更改为以下内容,我成功实现了我的目标:

 $gebruikers = Gebruiker::whereHas('maaltijdregistraties', function ($query) {
            $query->groupBy('datum')
            ->havingRaw('COUNT(*) > 1')
        
   ; })
   
->with(['maaltijdregistraties' => function ($query) {
    $query->groupBy('datum')
    ->havingRaw('COUNT(*) > 1')
    
    ;
}])
    ->get();

The blade code is刀片代码是

<ul>
            @foreach ($gebruikers as $gebruiker)
                <li>{{$gebruiker->first}} {{$gebruiker->last}} {{$gebruiker->maaltijdregistraties->groupBy('datum')->count()}}</li>
                
            @endforeach
           </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM