Browse Source

去掉模型的->where('status', ProductStatus::ON_SALE);列表增加whereHas判断

dev
李可松 4 years ago
parent
commit
486eef337e
  1. 9
      app/Http/Controllers/Api/AgentProductController.php
  2. 2
      app/Models/AgentProduct.php

9
app/Http/Controllers/Api/AgentProductController.php

@ -25,6 +25,9 @@ class AgentProductController extends Controller
$list = AgentProduct::where($where)
->with('product:id,title,pictures')
->whereHas('product', function ($query) {
return $query->where('status', ProductStatus::ON_SALE);
})
->select('id', 'sale', 'product_id', 'price', 'original_price')
->orderBy('id', 'DESC')
->simplePaginate();
@ -41,6 +44,9 @@ class AgentProductController extends Controller
->with('product:id,title,pictures,know,stock,content')
->with('coupon:tag,agent_product_id')
->with('fav:agent_product_id')
->whereHas('product', function ($query) {
return $query->where('status', ProductStatus::ON_SALE);
})
->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]);
if (!$agent_product || !$agent_product->product) {
@ -66,6 +72,9 @@ class AgentProductController extends Controller
// TODO 此处需要再优化排序规则,并增加广告功能
$list = AgentProduct::where('agent_id', $this->agent_id)
->with('product:id,title,pictures')
->whereHas('product', function ($query) {
return $query->where('status', ProductStatus::ON_SALE);
})
->select('id', 'sale', 'product_id', 'price', 'original_price')
->orderBy('id', 'DESC')
->simplePaginate();

2
app/Models/AgentProduct.php

@ -14,7 +14,7 @@ class AgentProduct extends BaseModel
public function product()
{
return $this->belongsTo(Product::class)->where('status', ProductStatus::ON_SALE);
return $this->belongsTo(Product::class);
}
public function coupon()

Loading…
Cancel
Save