使用laravel编辑用户信息

时间:2018-09-27 17:20:02

标签: laravel laravel-5 eloquent laravel-migrations

我已经在我的网站中为管理员建立了一个cms界面。管理员可以使用表格添加\编辑用户信息。 当我发送编辑表单时,我不断收到此错误:Column not found: 1054 Unknown column 'updated_at' in 'field list',这表明数据库更新正在尝试保存所有请求索引(其中包含来自其他表的列的值),而不仅仅是我自己的尝试更新。

我已设法将问题跟踪到$user_role->save();行中。 上面的行完成了应做的工作(找到相应的user_role并更改其值)。

这是我的代码

模型

static public function update_user($request, $id){
        
        $image_name = '';

        if( !empty($request['profile_image']) && $request->hasFile('profile_image') && $request->file('profile_image')->isValid() ){

            $file = $request->file('profile_image');
            $image_name = date('Y.m.d.H.i.s') . '-' . $file->getClientOriginalName();
            $request->file('profile_image')->move( public_path() . '/images/profile-images/' , $image_name);
            $img = Image::make( public_path() . '/images/profile-images/' . $image_name );
            $img->resize(370, null, function ($constraint) {
                $constraint->aspectRatio();
            });
            $img->save();

        }
        $user = self::find($id);
        $user->name = $request['name'];
        $user->email = $request['email'];
        $user->phone = $request['phone'];
        if( !empty($request['password']) ){
            $user->password = bcrypt($request['password']);
        }
        if(!empty($image_name)){
            $user->profile_image = $image_name;
        }
        if( !empty($request['role_id']) ){
            $user_role = Users_role::find($id);
            $user_role->role_id = $request['role_id'];
            $user_role->save();
        }
        $user->save();
        Session::flash('sm', 'Your profile has been updated');
        Session::flash('sm-position', 'toast-top-center');
        Session::put('user_name', $request['name']);
    }

查看

<div class="row">
    <div class="span9">
        <div class="content">
            <div class="module message">
                <div class="module-head">
                    <h3><b>Edit Product</b></h3>
                </div><br>
                    <div class="content">
                        <div class="module message">
                            <div class="module-body">
                                    <form action="{{ url('cms/users/' . $user->id) }}" method="POST" novalidate="novalidate" autocomplete="off" enctype="multipart/form-data">
                                        <div class="module-body">
                                            @method('PUT')
                                            @csrf
                                            <input type="hidden" name="user_id" value="{{ $user->id}}">
                                    <div class="form-group">
                                        <div class="input-group mb-3">
                                            <div class="w-100 field-input-cms">
                                                <label for="category-id" class="input-group-text h-50"><span class="text-danger">*</span><b> Permissions:</b></label>
                                                <select name="role_id" class="custom-select span-8">
                                                    <option @if ( $user->role_id == 8 ) selected="selected" @endif value="8">Admin</option>
                                                    <option @if ( $user->role_id == 2 ) selected="selected" @endif value="2">Regular</option>
                                                </select>
                                            </div>
                                            <small class="text-muted help-text">Please select one option</small><br>
                                            <span class="text-danger">&nbsp; {{ $errors->first('category_id') }}</span>
                                        </div>
                                        <div class="w-100 field-input-cms">
                                            <label for="name" class="input-group-text h-100"><span class="text-danger">*</span><b> Name:</b></label>        
                                            <input type="text" value="{{ $user->name }}" name="name" style="width:100%" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-default">
                                        </div>
                                        <small class="text-muted help-text">Name of user</small><br>
                                        <span class="text-danger">&nbsp; {{ $errors->first('name') }}</span>
                                        <div class="field-input-cms w-100">
                                            <label for="email" class="input-group-text"><span class="text-danger">*</span><b> Email:</b></label>        
                                            <input type="text" value="{{ $user->email }}" name="email" size="120" class="form-control mw-100" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-default">
                                        </div>
                                        <small class="text-muted text-balck help-text"> Email of user</small><br>
                                        <span class="text-danger">&nbsp; {{ $errors->first('email') }}</span>
                                        <div class="field-input-cms w-100">
                                            <label for="phone" class="input-group-text"><span class="text-danger">*</span><b> Phone:</b></label>        
                                            <input type="text" value="{{ $user->phone }}" name="phone" size="120" class="form-control mw-100" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-default">
                                        </div>
                                        <small class="text-muted text-balck help-text"> Phone number of user</small><br>
                                        <span class="text-danger">&nbsp; {{ $errors->first('phone') }}</span>
                                            <div class="input-group mb-3">
                                                <div class="input-group-prepend">
                                                    <span class="input-group-text" id="inputGroupFileAddon01">Upload</span>
                                                </div>
                                                <div class="custom-file">
                                                    <input type="file" name="profile_image" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
                                                    <label class="custom-file-label" name="profile_image" for="inputGroupFile01">Choose file</label>
                                                </div>
                                            </div>
                                            <small class="text-muted help-text">Image must be: jpg, jpeg, png, gif. Max: 5mb</small><br>
                                            <span class="text-danger">&nbsp; {{ $errors->first('profile_image') }}</span>
                                        </div>
                                        <div class="form-group">
                                            <img id="cms-profile-image" src="{{ asset('images/' . $user->profile_image) }}" >
                                        </div><br>
                                        <a class="btn btn-inverse" href="{{ url('cms/users') }}">Cancel</a>
                                        <input type="submit" value="Save Product" name="submit" class="btn btn-primary">
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
            </div>        
        </div> <!--/.content-->
    </div><!--/.span9-->
</div>

laravel给出的错误图片

enter image description here

我应该提一下,如果我注释掉这段代码:

        if( !empty($request['role_id']) ){
            $user_role = Users_role::find($id);
            $user_role->role_id = $request['role_id'];
            $user_role->update();
        }
所有值都正确保存。

1 个答案:

答案 0 :(得分:5)

如果您的users表中没有created_atupdated_at列,则应设置:

public $timestamps = false;

在您的User模型中。

Laravel默认情况下假定您具有表的那些字段。因此,无论何时创建/更新记录,它都会自动设置/更新这些字段。

或者,您可以更新表结构以添加这些字段,然后Laravel将自动处理这些字段(在这种情况下,请勿将timestamps设置为false)。

您可能有兴趣阅读有关Eloquent conventions

的信息