Laravel 在@foreach中提供注册的总价

Laravel 在@foreach中提供注册的总价,laravel,Laravel,我有以下查询,以获取用户在会议中的下一次注册: $nextRegistrations = $user->registrations()->whereHas('conference', function ($query) { $query->where('end_date', '>', now()); })->paginate($pageLimit); 然后显示用户每次注册的数据: @foreach($nextRegistra

我有以下查询,以获取用户在会议中的下一次注册:

$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

在注册模型中定义此函数

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($nextRegistration作为$nextRegistration)
@如果(!empty($nextRegistration->conference)| |!empty($nextRegistration->conference->start_date))
{{可选($nextRegistration->conference)->name}
...
@恩迪夫
@如果($nextRegistration->totalPrice>0)
@恩迪夫
@endforeach

如果您不想计算SQL查询本身的总价,实现这一点的一种方法是-

注册的
总价
取决于
参与者
注册类型
。因此,请在初始查询中加载这些关系,以便在
sum
计算期间可用

因此,问题是:

$nextRegistrations = $user->registrations()
                          ->with('participants.registration_type')
                          ->whereHas(
                              'conference', 
                              function ($query) {
                                  $query->where('end_date', '>', now());
                              }
                           )->paginate($pageLimit);
现在,定义名为
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;
    });
}
现在,您可以通过
$registration->totalPrice
从已加载的
$registration
模型访问
totalPrice
。拉威尔会帮你打电话给访问者。您甚至可以为每个
$registration
模型自动加载
totalPrice
字段,方法是将该字段添加到
$appends
数组中,添加到
registration
模型中

/**
 * The accessors to append to the model's array form.
 *
 * @var array
 */
protected $appends = ['totalPrice'];
这在序列化(即从API提供服务)期间非常有用

$nextRegistrations = $user->registrations()
                          ->with('participants.registration_type')
                          ->whereHas(
                              'conference', 
                              function ($query) {
                                  $query->where('end_date', '>', now());
                              }
                           )->paginate($pageLimit);
/**
 * 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;
    });
}
/**
 * The accessors to append to the model's array form.
 *
 * @var array
 */
protected $appends = ['totalPrice'];