Content.php 933 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Content extends Model
  5. {
  6. protected $table = 'contents';
  7. protected $guarded = [];
  8. protected $types;
  9. public function __construct(array $attributes = [])
  10. {
  11. parent::__construct($attributes);
  12. $this->types = collect([
  13. collect(['key' => 1, 'value' => 1, 'show_value' => '公告']),
  14. collect(['key' => 2, 'value' => 2, 'show_value' => '活动']),
  15. collect(['key' => 3, 'value' => 3, 'show_value' => '视频']),
  16. collect(['key' => 4, 'value' => 4, 'show_value' => '文章']),
  17. ]);
  18. }
  19. public function getModelType($type)
  20. {
  21. return in_array($type, [1, 2, 3, 4]) ? $type : 1;
  22. }
  23. public function getModelName($type)
  24. {
  25. $item = $this->types->where('key', $type)->first();
  26. return empty($item) ? '公告' : $item['show_value'];
  27. }
  28. }