【问题标题】:How to show the minimum and maximum price?如何显示最低和最高价格?
【发布时间】:2018-03-16 20:42:31
【问题描述】:

我有一个显示特定会议详细信息(名称、描述、价格等)的视图。

显示我使用的每个信息:{{$conf->name}}, {{$conf->descrption}}, etc.

但在价格的情况下,会议没有价格。每个会议可以有多种注册类型。每种注册类型都有特定的价格。

价格是 RegistrationType 表的列,而不是 Conference 表的列。

在会议详情视图中我想显示价格范围,例如,如果会议有 3 种注册类型,并且其中一种注册类型的最低价格为“0”,最高价格为“10”,我想在视图中显示该会议的注册类型范围,即“0-10”。

但我不明白如何在视图中显示特定会议的注册类型的范围价格,你知道吗?

Conference 模型和 RegistrationType 模型如下:

会议模型:

class Event extends Model
{
// A conference has many registration types
    public function registrationTypes(){
        return $this->hasMany('App\RegistrationType', 'conference_id');
    }
}   

RegistrationType 模型:

class RegistrationType extends Model
{
    public function conference(){
        return $this->belongsTo('App\Conference');
    }
}

【问题讨论】:

  • 您可以执行 $conference->registrationTypes() 之类的操作并获取所有注册类型的集合。您还可以将其包装在会议(事件)模型本身的辅助方法中以计算范围。

标签: php laravel


【解决方案1】:

我会这样做(假设 Event 实际上是您的 Conference 课程):

class Conference extends Model
{
    protected $appends = ['price_range'];

    public function registrationTypes(){
        return $this->hasMany('App\RegistrationType', 'conference_id');
    }

    public function getPriceRangeAttribute() {
        return $this->registrationTypes()->get()->min('price') . ' - ' . $this->registrationTypes()->get()->max('price');
        /* The first answer :
         * return $this->registrationTypes()->min('price') . ' - ' . $this->registrationTypes()->max('price');
         * should also do it, and could even be better, because no 
         * other results than the actual min and max are returned,
         * no collection fetched ! */
    }
}

在这里你告诉 Laravel appends price_range 属性到每个 Conference 实例。 price_range 属性由 getPriceRangeAttribute accessor 返回,该属性检查在价格列中找到的每个 registrationType 链接(由 hasMany 关系返回)到您的会议。

【讨论】:

  • 谢谢,但就像在前端使用“{{$conf->price_range}}”出现“string(148)”select min(price) 作为聚合来自registration_types where @ 987654326@.conf_id = ?并且registration_types.conf_id 不为空" string(148) " 选择最大值(price) 作为来自registration_types 的聚合,其中registration_types.conf_id = ?而registration_types.conf_id 不为空“2 - 10”,而不是显示价格范围。你知道为什么吗?
  • 现在在“返回 $this->registrationTypes()->min('price')->get() 上显示“调用成员函数 get() on float”。' - ' .$this->registrationTypes()->max('price')->get();".
  • 重新编辑。我相信 get 方法必须在 min 和 max 方法之前调用
  • 谢谢,但同样的问题再次出现:“string(124)”select * from registration_types where registration_types.conf_id = ?并且registration_types.conf_id 不为空" string(124) "select * from registration_types where registration_types.conf_id = ?并且registration_types.conf_id 不为空“2 - 10”。
  • 您是否在某处记录您的查询或关系?用 print_r 什么的?
【解决方案2】:

示例代码类似于:

class Event extends Model
{
    public function registrationTypes(){
        return $this->hasMany('App\RegistrationType', 'conference_id');
    }

    public function priceRangeAttribute() {
        $registrationTypes = $this->registrationTypes();
        return $this->orderPriceRanges($registrationTypes);
    }

    private function orderPriceRanges($registrationTypes) {
        // process the collection and format the different prices (0, 2, 10)
        // into a price range like 0-10
    }
}

然后您可以像 $conf->price_range 一样访问它。

【讨论】:

  • 谢谢,但像这样出现“调用未定义的方法 Illuminate\Database\Query\Builder::registrationPriceRange”。你知道为什么吗?
  • 对不起,我把它写在我的头上。我通过将方法重命名为访问器 laravel.com/docs/5.6/eloquent-mutators#defining-an-accessor 以及您可以调用它的方式来更新示例。
  • 谢谢!!在 orderPriceRanges 我有私有函数 orderPriceRanges($registrationTypes) { foreach ($registrationTypes as $rtype){ return "price"; } } 进行测试,但在视图中没有出现。你知道为什么吗?
  • 尝试使用调试器或简单的dd($registrationTypes); 调用来调试变量的值。
  • 您也可以在 $registrationTypes 上使用集合方法,因为它应该是一个集合。例如,您可以$registrationTypes->sortBy('price')->pluck('price'); 获取订购价格。然后你可以return $orderedPrices->min() . "-" . $orderedPrices->max()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-13
相关资源
最近更新 更多