Resource.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Models\Model;
  4. use Illuminate\Support\Facades\App;
  5. use Prettus\Repository\Contracts\Transformable;
  6. use Prettus\Repository\Traits\TransformableTrait;
  7. /**
  8. * Class Resource.
  9. *
  10. * @package namespace App\Repositories\Models;
  11. */
  12. class Resource extends Model implements Transformable
  13. {
  14. protected $table = 'base_resources';
  15. use TransformableTrait;
  16. /**
  17. * The attributes that are mass assignable.
  18. *
  19. * @var array
  20. */
  21. protected $fillable = ['name', 'original_name', 'path', 'disk', 'url', 'size', 'status'];
  22. protected static function booted()
  23. {
  24. $language = 'zh_CN';
  25. switch (App::getLocale()) {
  26. case 'zh_CN':
  27. default:
  28. $language = 'zh_CN';
  29. break;
  30. case 'en':
  31. $language = 'en';
  32. break;
  33. }
  34. $language = 'en';
  35. static::creating(function ($model) use ($language) {
  36. $model->language = $language;
  37. });
  38. static::updating(function ($model) use ($language) {
  39. $model->language = $language;
  40. });
  41. }
  42. public function getUrlAttribute($val)
  43. {
  44. if (isset($this->attributes['disk']) && $this->attributes['disk'] == 'public') {
  45. return path_to_url($this->attributes['path']);
  46. }
  47. return $val;
  48. }
  49. }