使用Laravel 5.4更新用户

时间:2017-03-29 10:55:21

标签: php database laravel-5.3

在我的laravel用户更新中需要您的帮助......

这是我的编码:

<form class="form-horizontal" role="form" method="POST" action="{{ route('editregister.post') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">Name</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control" name="name" value="{{ $users->name }}" required autofocus>
                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
                            <label for="username" class="col-md-4 control-label">Username</label>
                            <div class="col-md-6">
                                <input id="username" type="username" class="form-control" name="username" value="{{ $users->username }}" required>
                                @if ($errors->has('username'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('username') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        @if ($users->position == "Super Admin")

                        @else
                            <div class="form-group{{ $errors->has('position') ? ' has-error' : '' }}">
                                <label for="user-position" class="col-md-4 control-label">Position</label>
                                <div class="col-md-6">
                                    <select id="position" class="form-control" name="position" required autofocus>
                                        <option value=""> -- Select Position -- </option>
                                        <option value="Admin" {{ $users->position == 'Admin' ? 'selected' : '' }}>Admin</option>
                                        <option value="User" {{ $users->position == 'User' ? 'selected' : '' }}>User</option>
                                    </select>
                                    @if ($errors->has('position'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('position') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>
                        @endif
                        @if(session()->has('message'))
                            <div class="alert alert-success">
                                {{ session()->get('message') }}
                            </div>
                        @endif
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Register
                                </button>
                                <a href='{{ url('/userlist') }}' class="btn btn-primary">
                                    Cancel
                                </a>
                            </div>
                        </div>
                    </form>

这是我的控制器:

public function editregister(Request $request, $iduser)
{

    $this->validate(request(), [
        'name' => 'required|max:255',
        'username' => 'required|max:255|unique:users',
        'position' => 'required',
        ]
    );      

    $name = $request->input('name');
    $username = $request->input('username');
    $position = $request->input('position');
    $updated_at = date("Y-m-d H:i:s");

    DB::table('users')->where('id', $iduser)->update([
        ['name' => $name, 'username' => $username, 'position' => $position, 'updated_at' => $updated_at, ]
    ]);

    return redirect()->back()->with('message', 'User is successfully Updated!');
}

这里我的网络路由编码:

Route::get('edituser/{iduser}',['as' => 'edituser', 'uses' => 'UserRegController@editregisterform']);
Route::post('editregister', ['as' => 'editregister.post', 'uses' => 'UserRegController@editregister']);

我不知道更新数据时如何运作...我是laravel 5.4的新用户......

请帮我解决这个问题..

2 个答案:

答案 0 :(得分:1)

更新时使用帖子。

Route::post('edituser/{iduser}',['as' => 'edituser', 'uses' => 'UserRegController@editregisterform']);

只需使用其ID $user = User::find($id)抓取用户对象 然后设置属性$user->name = 'updated name',然后设置$user->save()。希望这会对你有所帮助。

答案 1 :(得分:0)

嗨谢谢你的回复...

我刚发现问题......

我删除了Request $ request

public function editregister(Request $request, $iduser)

并且这样做:

public function editregister($iduser)

然后它修复了......