From 486eef337ef488988e5f6e395df9d6f00d3ed1ee Mon Sep 17 00:00:00 2001 From: liapples Date: Wed, 18 Aug 2021 17:29:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=A8=A1=E5=9E=8B=E7=9A=84->?= =?UTF-8?q?where('status',=20ProductStatus::ON=5FSALE);=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0whereHas=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/AgentProductController.php | 9 +++++++++ app/Models/AgentProduct.php | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/AgentProductController.php b/app/Http/Controllers/Api/AgentProductController.php index 80161b4..59c2f0d 100644 --- a/app/Http/Controllers/Api/AgentProductController.php +++ b/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(); diff --git a/app/Models/AgentProduct.php b/app/Models/AgentProduct.php index 56f235b..cb15a9c 100644 --- a/app/Models/AgentProduct.php +++ b/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()