【发布时间】: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