AdminConfig.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class AdminConfig extends Model
  5. {
  6. protected $fillable = ['name', 'config_key', 'config_value', 'type'];
  7. protected $connection = 'write';
  8. public static function getValue($key)
  9. {
  10. $instance = new static;
  11. if (is_array($key)) {
  12. $result = $instance->whereIn('config_key', $key)->get();
  13. if ($result) {
  14. $data = $result->flatMap(function ($config) {
  15. return [$config->config_key => $config->config_value];
  16. })->toArray();
  17. } else {
  18. $data = [];
  19. }
  20. return $data;
  21. } else {
  22. $result = $instance->where('config_key', $key)->first();
  23. if (!$result) {
  24. return null;
  25. }
  26. return $result->config_value;
  27. }
  28. }
  29. public function scopeSearchCondition($query, string $keyword = null)
  30. {
  31. if (is_null($keyword)) {
  32. return $query;
  33. } else {
  34. return $query->where("config_key", "like", "%{$keyword}%")
  35. ->orWhere("name", "like", "%{$keyword}%");
  36. }
  37. }
  38. }