Php 如何显示最低和最高价格?

Php 如何显示最低和最高价格?,php,laravel,Php,Laravel,我有一个视图可以显示特定会议的详细信息(名称、描述、价格等) 要显示我使用的每个信息:{{$conf->name}、{{{$conf->descrption}等等。 但就价格而言,会议没有价格。每个会议可以有多种注册类型。每种注册类型都有特定的价格 价格是RegistrationType表的一列,而不是Conference表 在会议详细信息视图中,我想显示价格范围,例如,如果会议有3种注册类型,其中一种注册类型的最低价格为“0”,最高价格为“10”,我想在视图中显示该会议的注册类型范围,即“0-

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

要显示我使用的每个信息:
{{$conf->name}、{{{$conf->descrption}等等。

但就价格而言,会议没有价格。每个会议可以有多种注册类型。每种注册类型都有特定的价格

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

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

但我不明白在视图中显示特定会议的注册类型的范围价格的过程是什么,你知道吗

会议模型和注册类型模型如下所示:

会议模式:

class Event extends Model
{
// A conference has many registration types
    public function registrationTypes(){
        return $this->hasMany('App\RegistrationType', 'conference_id');
    }
}   
class RegistrationType extends Model
{
    public function conference(){
        return $this->belongsTo('App\Conference');
    }
}
注册类型型号:

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

示例代码类似于:

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\u range一样访问它。

我会这样做(假设该事件实际上是您的会议类):


在这里,您告诉Laravel将价格范围属性应用于会议的每个实例price\u range属性由getPriceRangeAttribute返回,该属性检查链接到会议的每个注册类型(由hasMany关系返回)在价格列中找到的最小值和最大值。

您可以执行类似$Conference->registrationTypes()的操作并获取所有注册类型的集合。您还可以将其包装在会议(事件)模型本身上的帮助器方法中,以计算范围。谢谢,但类似于出现的“调用未定义的方法照亮\Database\Query\Builder::registrationPriceRange”。你知道为什么吗?对不起,我只是想随便说说。我更新了这个示例,将方法重命名为访问器,以及您可以调用它的方式。谢谢!!在orderPriceRanges中,我有私有函数orderPriceRanges($registrationTypes){foreach($rtype的registrationTypes){return“price”;}}}要测试,但在视图中没有显示任何内容。你知道为什么吗?试着用调试器或简单的
dd($registrationTypes)调试变量的值调用。您还可以对$registrationTypes使用收集方法,因为它应该是一个集合。例如,您可以
$registrationTypes->sortBy('price')->pull('price')获取订单价格。然后您可以
返回$orderedPrices->min()。"-" . $orderedPrices->max()
。谢谢,但是就像前端一样,使用“{{$conf->price\u range}”出现“string(148)”选择min(
price
)作为从
registration\u types
其中
registration\u types
conf\u id
=?和
registration\u types
conf\u id
不为空“string(148)”从
registration\u types
中选择max(
price
)作为聚合,其中
registration\u types
conf\u id
=?并且
注册类型
配置id
不为空“2-10”,而不是显示价格范围。你知道为什么吗?现在在“return$this->registrationTypes()->min('price')->get().-'。$this->registrationTypes()->max('price')->get();”上出现了“在float上调用成员函数get()”。重新编辑。我相信get方法必须在min和max方法hanks之前调用,但同样的问题再次出现:“string(124)”select*from
registration\u types
where
registration\u types
conf\u id
=?而
注册类型
配置id
不为空“字符串(124)”从
注册类型
中选择*其中
注册类型
配置id
=?并且
注册类型
配置id
不为空“2-10”。您是否在某处记录查询或关系?用印刷品什么的?