在@foreach中有注册的总价格

时间:2018-07-08 17:02:07

标签: laravel

我在下面使用此查询来获取用户在会议中的下一个注册:

$nextRegistrations = $user->registrations()->whereHas('conference', function ($query) {
            $query->where('end_date', '>', now());
        })->paginate($pageLimit);

然后显示用户每次注册的数据:

@foreach($nextRegistrations as $nextRegistration)
    @if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
        <h5>{{optional($nextRegistration->conference)->name}}</h5>
        ...
    @endif
@endforeach

仅当注册的总价格为“ 0”时,我才想显示“删除注册”按钮,例如:

@if ($nextRegistration->totalPrice > 0)
    <a href="{{route('registration.remove', ['regID'=> $nextRegistration->id])}}" class="btn btn-outline-primary"> Remove</a>
@endif 

要获取特定注册的总价,查询可以如下所示。但是,您知道如何在$ nextRegistrations查询中使用此代码,以便$ nextRegistrations在@foreach中获得注册总价的信息吗?

$registration = Registration::with('participants', 'participants.registration_type')->find($regID);

$total = $registration->participants->sum(function ($participant) {
    return $participant->registration_type->price;
});

2 个答案:

答案 0 :(得分:1)

如果您不想在SQL查询本身上计算总价,则实现此目标的一种方法是-

正如您在代码中指出的,totalPrice的{​​{1}}取决于registrationparticipants。因此,急切地用初始查询加载这些关系,以使其在registration_type计算期间可用。

因此查询将是:

sum

现在,像这样定义名为$nextRegistrations = $user->registrations() ->with('participants.registration_type') ->whereHas( 'conference', function ($query) { $query->where('end_date', '>', now()); } )->paginate($pageLimit); 的{​​{1}}模型的访问器:

Registration

现在,您可以通过totalPrice从已经加载的/** * Get the total price attribute of the registration. * * @return float $totalPrice */ public function getTotalPriceAttribute() { // Here the assumption is `participants` relation will // be available by eager load to prevent n+1 query return $this->participants->sum(function ($participant) { return $participant->registration_type->price; }); } 模型中访问totalPrice。 Laravel将为您调用访问器。您甚至可以通过将其添加到$registration模型的$registration->totalPrice数组中,为每个totalPrice模型自动加载$registration字段。

$appends

这在序列化(即通过API服务)期间非常有用。

答案 1 :(得分:0)

在您的注册模型中定义此功能

public function getTotalPriceAttribute() {

    // if relation is not loaded already, let's do it first
    if ( ! array_key_exists('participants', $this->relations)) 
         $this->load('participants', 'participants.registration_type');      

    return $this->participants->sum(function ($participant) {
        return $participant->registration_type->price;
    });
}

在控制器中

$nextRegistrations = Registration::with('participants', 'participants.registration_type')
                     ->where('user_id', $user->id)
                     ->whereHas('conference', function ($query) {
                         $query->where('end_date', '>', now());
                     })->paginate($pageLimit);

查看

@foreach($nextRegistrations as $nextRegistration)
    @if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
        <h5>{{optional($nextRegistration->conference)->name}}</h5>
        ...
    @endif
    @if ($nextRegistration->totalPrice > 0)
        <a href="{{route('registration.remove', ['regID'=> $nextRegistration->id])}}" class="btn btn-outline-primary"> Remove</a>
    @endif 
@endforeach