OwnerPriceLogistic.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. class OwnerPriceLogistic extends Model
  6. {
  7. protected $fillable = [
  8. "name", //名称
  9. "unit_range", //单价一区间
  10. "unit_id", //单位一ID
  11. "other_unit_range", //单位二区间
  12. "other_unit_id", //单位二ID
  13. "pick_up_price", //提货费
  14. "fuel_price", //燃油附加费
  15. "service_price", //信息服务费
  16. ];
  17. public function getUnitRangeJsonAttribute()
  18. {
  19. return json_encode(explode(",",$this->unit_range));
  20. }
  21. public function getOtherUnitRangeJsonAttribute()
  22. {
  23. return json_encode(explode(",",$this->other_unit_range));
  24. }
  25. public function details()
  26. {
  27. return $this->hasMany(OwnerPriceLogisticDetail::class,"owner_price_logistic_id","id");
  28. }
  29. public function unit()
  30. { //单位一
  31. return $this->hasOne(Unit::class,"id","unit_id");
  32. }
  33. public function otherUnit()
  34. { //单位二
  35. return $this->hasOne(Unit::class,"id","other_unit_id");
  36. }
  37. public function owners()
  38. { //货主
  39. return $this->belongsToMany(Owner::class,"owner_price_logistic_owner","owner_price_logistic_id","owner_id");
  40. }
  41. public function logistics()
  42. { //物流
  43. return $this->belongsToMany(Logistic::class,"owner_price_logistic_logistic","owner_price_logistic_id","logistic_id");
  44. }
  45. public function getOwnerIdAttribute()
  46. { //获取货主ID数组
  47. return array_column(DB::select(DB::raw("SELECT * FROM owner_price_logistic_owner WHERE owner_price_logistic_id = ?"),[$this->id]),"owner_id");
  48. }
  49. public function getLogisticIdAttribute()
  50. { //获取快递ID数组
  51. return array_column(DB::select(DB::raw("SELECT * FROM owner_price_logistic_logistic WHERE owner_price_logistic_id = ?"),[$this->id]),"logistic_id");
  52. }
  53. }