From a2a860f63ebc4fb4cce7109773aacfcc2127ce42 Mon Sep 17 00:00:00 2001 From: liapples Date: Fri, 20 Aug 2021 18:00:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0scopeList=E4=BD=9C=E7=94=A8?= =?UTF-8?q?=E5=9F=9F=E6=9D=A5=E8=8E=B7=E5=8F=96=E4=BA=A7=E5=93=81=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/AgentProduct.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/Models/AgentProduct.php b/app/Models/AgentProduct.php index 2f9aa97..6138030 100644 --- a/app/Models/AgentProduct.php +++ b/app/Models/AgentProduct.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Common\ProductStatus; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; @@ -64,4 +65,16 @@ class AgentProduct extends BaseModel $this->attributes['product_ids'] = join(',', array_filter($value)); } } + + //列表查询统一查询条件 + public function scopeList($query) + { + return $query->with('product:id,title,pictures') + ->whereHas('product', function ($query) { + return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0); + }) + ->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0) + ->select('id', 'sale', 'product_id', 'price', 'original_price') + ->orderBy('id', 'DESC'); + } }