Service.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. class Service
  14. {
  15. protected static function toModel($attributes, $class)
  16. {
  17. $classInstance = app($class);
  18. isset($attributes['id']) && ($classInstance = $classInstance->find($attributes['id']));
  19. // $oldAttributes = $classInstance->getAttributes();
  20. // $newAttributes = array_merge($oldAttributes, $attributes);
  21. // $classInstance->setRawAttributes($newAttributes);
  22. collect($attributes)->each(function ($value, $key) use ($classInstance) {
  23. $classInstance->setAttribute(Str::snake($key), $value);
  24. });
  25. return $classInstance;
  26. }
  27. /**
  28. * @throws ApiException
  29. */
  30. protected static function error(string $message = '系统错误', int $code = 500)
  31. {
  32. $data = ['code' => $code, 'message' => $message];
  33. throw new ApiException($data);
  34. }
  35. }