OwnerPriceLogistic.php 2.0 KB

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