【问题标题】:Laravel Model - Function to return highest and lowest priceLaravel 模型 - 返回最高和最低价格的函数
【发布时间】:2020-01-28 10:15:14
【问题描述】:

在 Laravel 中,我有一个 Offer 模型和 OfferPrice 模型。报价有一个“个人”列,用于标识价格是“个人”价格还是“企业”价格。我想做的是调用具有价格关系的报价并返回“最高个人价格”、“最低个人价格”、“最高商业价格”和“最低商业价格”。

我的想法是这样称呼:

Offer::with('Information', 'OfferPrice.highestPersonalPrice', 'OfferPrice.lowestPersonalPrice', 'OfferPrice.highestBusinessPrice', 'OfferPrice.lowestBusinessPrice')->get();

这会返回如下内容:

{
    "id": 1,
    "name": "test offer",
    "description": "offer description",
    "Information": {
        "source": "Contact 1",
        "status": "3"
    },
    "max_personal_price": 1345.32,
    "min_personal_price": 456.43,
    "max_business_price": 1245.32,
    "min_business_price": 432.32
}

这是当前 offer_prices 表的示例:

offer_id .   deposit .   price .  personal .   status .   expiry
1            300         1345.32  1            1          null
1            200         456.43   1            1          null
1            250         950.32   1            1          null
1            150         740.32   0            1          null
1            200         432.32   0            1          null
1            250         1245.32  0            1          null          

当前报价模式:

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class Offer extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'info_id','approved','expiry','branch_id'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */

    protected $hidden = ['deleted_at'];

    public $timestamps = true;

    protected $dates = [
        'deleted_at'
    ];

    public function prices()
    {
        return $this->hasMany('App\OfferPrice');
    }

    public function information()
    {
        return $this->belongsTo('App\Information');
    }

}

OfferPrice 模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class OfferPrice extends Model {

    protected $fillable = ['offer_id', 'deposit', 'price', 'personal', 'expiry', 'status'];

    protected $hidden = ['status', 'offer_id', 'created_at', 'updated_at', 'deleted_at'];

    protected $primaryKey = ['offer_id', 'deposit','personal'];

    public $incrementing = false;

    public $timestamps = true;

    public function offers()
    {
        return $this->belongsTo('App\Offer');
    }

}

我已经尝试了各种不同的方法来尝试做到这一点,但还没有什么能接近做到这一点。希望那里有一位知道他们的东西的大师:)

【问题讨论】:

  • 业务不在模型中,业务在personal = 0处被识别

标签: php laravel eloquent model relationship


【解决方案1】:

无法非常正确地理解您的问题,但如果您想要 OfferPrice 模型的最高和最低价格,它将是 OfferPrice::max('price')OfferPrice::min('price'),如果使用来自 Offer 模型的关系,它将是 Offer::first()->prices->max('price')Offer::first()->prices->min('price')我认为您从我的回答中得到了一些想法,您可以尝试让我知道,如果我没有理解您的问题,请评论我的回答或编辑您的问题。

在 OfferPrice 模型中

public function scopePersonal($query, $param) {
    return $query->where('personal', 1);
}

$offer::(‘Information’)->prices()->personal(1)->max(‘price’);

【讨论】:

  • 我对您的解决方案遇到的问题是,它为您提供了最大值和最小值,而不是业务/个人最大值/最小值。感谢您的回复。
  • @Paul 你能用personal 代替price 吗?
  • 它需要类似于 $offer::('Information')->prices->where('personal', '=', 1)->max('price')- >as('max_personal_price')->prices->where('personal', '=', 1)->min('price')->as('min_personal_price')->prices->where('personal' , '=', 0)->max('price')->as('max_business_price')->prices->where('personal', '=', 0)->min('price')-> as('min_business_price')->get();但不能让这样的事情发挥作用。这是因为它们需要以列表和单个实例的形式返回。
  • @Paul 我想我遇到了你的问题,只需创建范围并应用它而不是 where 子句,这应该可以正常工作。
  • @Paul 编辑了我的答案,周围的东西应该适合你。
猜你喜欢
  • 2021-03-25
  • 2023-03-14
  • 1970-01-01
  • 2019-06-13
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
相关资源
最近更新 更多