海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.6 KiB

4 years ago
  1. <?php
  2. namespace App\Models;
  3. use App\Common\ProductStatus;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. class AgentProduct extends BaseModel
  7. {
  8. use HasFactory, SoftDeletes;
  9. protected $guarded = ['id'];
  10. public function product()
  11. {
  12. return $this->belongsTo(Product::class);
  13. }
  14. public function coupon()
  15. {
  16. return $this->hasMany(Coupon::class);
  17. }
  18. public function fav()
  19. {
  20. return $this->hasOne(UserFav::class);
  21. }
  22. public function agent()
  23. {
  24. return $this->belongsTo(Agent::class);
  25. }
  26. public function category()
  27. {
  28. return $this->belongsTo(Category::class);
  29. }
  30. public function user()
  31. {
  32. return $this->hasOne(User::class, 'id', 'verifier');
  33. }
  34. public function guide()
  35. {
  36. return $this->belongsTo(Guide::class);
  37. }
  38. public function item()
  39. {
  40. return $this->hasMany(AgentProductItem::class);
  41. }
  42. public function setChannelIdAttribute($value)
  43. {
  44. if (is_array($value)) {
  45. $this->attributes['channel_id'] = join(',', array_filter($value));
  46. }
  47. }
  48. public function setProductIdsAttribute($value)
  49. {
  50. if (is_array($value)) {
  51. $this->attributes['product_ids'] = join(',', array_filter($value));
  52. }
  53. }
  54. //列表查询统一查询条件
  55. public function scopeList($query)
  56. {
  57. return $query->with('product:id,title,pictures')
  58. ->whereHas('product', function ($query) {
  59. return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0);
  60. })
  61. ->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0)
  62. ->select('id', 'sale', 'product_id', 'price', 'original_price')
  63. ->orderBy('id', 'DESC');
  64. }
  65. }