目前,我有3款车型,Listing, the & Payment,它们有以下几种关系:
列表
class Listing extends Model {
public function offers() {
return $this->hasMany(\App\Models\Offer::class)->orderBy('created_at', 'desc');
}
}提供
class Offer extends Model {
public function payment() {
return $this->hasOne(\App\Models\Payment::class, 'item_id', 'id')->where('item_type', \App\Models\Offer::class)->where('status', '1');
}
public function listing() {
return $this->belongsTo(\App\Models\Listing::class)->withTrashed();
}
}付款
class Payment extends Model {
public function offer() {
return $this->belongsTo(\App\Models\Offer::class, 'item_id', 'id')->withTrashed();
}
}我如何才能从他们列表模型&直接返回与支付表的关系?
Listing可以有无限的报价,但Listing只能有1最大支付
要找到任何相应的支付信息,我必须查询基于模型中的 listing_id的listing_id,然后访问Offer->payment,当我更希望能够这样做时:
$transaction_id = $id;
$listing = Listing::whereHas('payment', function($q) use ($id) {
$q->where('transaction_id', $id);
$q->where('user_id', Auth::user()->id);
})->first();发布于 2018-07-26 23:39:57
public function payments() {
return $this->hasManyThrough(Payment::class, Offer::class, null, 'item_id')
->where('payments.item_type', Offer::class)
->where('payments.status', '1')
->orderBy('offers.created_at', 'desc');
} https://stackoverflow.com/questions/51548712
复制相似问题