Good.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Repositories\Models\Dwbs;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Model;
  5. use BaconQrCode\Common\Mode;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Facades\Cache;
  8. class Good extends Model
  9. {
  10. /**
  11. * @var string
  12. */
  13. protected $table = 'dwbs_goods';
  14. protected $guarded = [];
  15. /**
  16. * The attributes excluded from the model's JSON form.
  17. *
  18. * @var array
  19. */
  20. protected $hidden = [];
  21. protected $casts = [];
  22. public function getCoverAttribute($val)
  23. {
  24. if (empty($val)) {
  25. return config('app.url') . '/default/headimg.png';
  26. }
  27. return path_to_url($val);
  28. }
  29. public function setCoverAttribute($val)
  30. {
  31. $this->attributes['cover'] = url_to_path($val);
  32. }
  33. public static function byId($id)
  34. {
  35. // return Cache::remember("model:good:byId:{$id}", Carbon::now()->addHours(1), function () use ($id) {
  36. return self::query()->where('id', $id)->where('status', ModelStatusEnum::OK)->first();
  37. // });
  38. }
  39. public static function getNames()
  40. {
  41. return Cache::remember("model:good:getNames", Carbon::now()->addHours(1), function () {
  42. return self::query()->where('status', ModelStatusEnum::OK)->select(['id', 'name'])->get();
  43. });
  44. }
  45. }