Trace.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Trace extends Model
  5. {
  6. public $fillable = ['key', 'gid'];
  7. public $appends = ['slug_value'];
  8. public function good()
  9. {
  10. return $this->belongsTo(Good::class, 'gid');
  11. }
  12. public function getKeyAttribute()
  13. {
  14. if ($this->attributes['id'] <= 50000) {
  15. return number_format(10000000 + $this->attributes['id'], 0, '', '');
  16. } else {
  17. return bcadd('1000000000000000', "{$this->attributes['id']}");
  18. }
  19. }
  20. public static function get_key($id)
  21. {
  22. if ($id <= 10050000) {
  23. $id = bcsub("{$id}", '10000000');
  24. } else {
  25. $id = bcsub("{$id}", '1000000000000000');
  26. }
  27. return $id;
  28. }
  29. public function dealer()
  30. {
  31. return $this->belongsTo(Dealer::class);
  32. }
  33. public function getSlugAttribute($value)
  34. {
  35. return config('app.url') . '/key/' . $value;
  36. }
  37. public function getSlugValueAttribute()
  38. {
  39. return $this->attributes['slug'];
  40. }
  41. public function setSlugValueAttribute($val)
  42. {
  43. $this->attributes['slug'] = $val;
  44. }
  45. }