Service.php 1.3 KB

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