zoukankan      html  css  js  c++  java
  • laravel 更新

    public function update(Request $request, ResponseFactoryContract $response)
        {
            $user = $request->user();
            $rules = [
                'name' => ['nullable', 'string', 'username', 'display_length:2,12'],
                'bio' => ['nullable', 'string'],
                'sex' => ['nullable', 'numeric', 'in:0,1,2'],
                'location' => ['nullable', 'string'],
                'avatar' => ['nullable', 'string', 'regex:/public:(.+)/is'],
                'bg' => ['nullable', 'string', 'regex:/public:(.+)/is'],
            ];
            $messages = [
                'name.string' => '用户名只能是字符串',
                'name.username' => '用户名只能以非特殊字符和数字开头,不能包含特殊字符',
                'name.display_length' => '用户名长度不合法',
                'bio.string' => '用户简介必须是字符串',
                'sex.numeric' => '发送的性别数据异常',
                'sex.in' => '发送的性别数据非法',
                'location.string' => '地区数据异常',
                'avatar.regex' => '头像错误',
                'bg.regex' => '背景图片错误',
            ];
            $this->validate($request, $rules, $messages);
    
            $target = ($name = $request->input('name'))
                ? $user->newQuery()->where('name', $name)->where('id', '!=', $user->id)->first()
                : null;
            if ($target) {
                return $response->json(['name' => ['用户名已被使用']], 422);
            }
            $fields = ['name', 'bio', 'sex', 'location', 'avatar', 'bg','true_name','true_name_set','company','occupation','industry','birthday','school','hometown','tagss'];
            foreach ($fields as $field) {
                if ($request->request->has($field)) {
                    $user->{$field} = $request->input($field);
                }
            }
    
            return $user->save()
                ? $response->make('', 204)
                : $response->json(['message' => ['更新失败']], 500);
        }
  • 相关阅读:
    边框
    文本样式
    框架
    表格
    列表
    标签
    常用类--包装类
    常见类 --Object
    日志
    异常
  • 原文地址:https://www.cnblogs.com/sgm4231/p/10543323.html
Copyright © 2011-2022 走看看