Browse Source

增加scopeList作用域来获取产品列表

dev
李可松 4 years ago
parent
commit
a2a860f63e
  1. 13
      app/Models/AgentProduct.php

13
app/Models/AgentProduct.php

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Common\ProductStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -64,4 +65,16 @@ class AgentProduct extends BaseModel
$this->attributes['product_ids'] = join(',', array_filter($value)); $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');
}
} }
Loading…
Cancel
Save