Service.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/8/15 14:35
  7. */
  8. namespace App\Http\Services;
  9. use App\Exceptions\ApiException;
  10. use App\Models\System\Menu;
  11. use Illuminate\Support\Arr;
  12. use Illuminate\Support\Str;
  13. use Spatie\Permission\Guard;
  14. class Service
  15. {
  16. protected string $guard_name = 'web';
  17. protected static function toModel($attributes, $class)
  18. {
  19. $classInstance = app($class);
  20. isset($attributes['id']) && filled($attributes['id']) && ($classInstance = $classInstance->find($attributes['id']));
  21. // $oldAttributes = $classInstance->getAttributes();
  22. // $newAttributes = array_merge($oldAttributes, $attributes);
  23. // $classInstance->setRawAttributes($newAttributes);
  24. collect($attributes)->each(function ($value, $key) use ($classInstance) {
  25. $classInstance->setAttribute(Str::snake($key), $value);
  26. });
  27. return $classInstance;
  28. }
  29. /**
  30. * @throws ApiException
  31. */
  32. protected static function error(string $message = '系统错误', int $code = 500)
  33. {
  34. $data = ['code' => $code, 'message' => $message];
  35. throw new ApiException($data);
  36. }
  37. }