12345678910111213141516171819202122232425262728293031323334353637383940 |
- <?php
- /**
- * @Name
- * @Description
- * @Author 刘学玺
- * @Date 2024/8/15 14:35
- */
- namespace App\Http\Services;
- use App\Exceptions\ApiException;
- use App\Models\System\Menu;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Str;
- class Service
- {
- protected static function toModel($attributes, $class)
- {
- $classInstance = app($class);
- isset($attributes['id']) && ($classInstance = $classInstance->find($attributes['id']));
- // $oldAttributes = $classInstance->getAttributes();
- // $newAttributes = array_merge($oldAttributes, $attributes);
- // $classInstance->setRawAttributes($newAttributes);
- collect($attributes)->each(function ($value, $key) use ($classInstance) {
- $classInstance->setAttribute(Str::snake($key), $value);
- });
- return $classInstance;
- }
- /**
- * @throws ApiException
- */
- protected static function error(string $message = '系统错误', int $code = 500)
- {
- $data = ['code' => $code, 'message' => $message];
- throw new ApiException($data);
- }
- }
|