简体   繁体   English

用户更新错误没有结果为模型auth /用户laravel 5

[英]User Update error no result for model auth/user laravel 5

im getting error when updating user details ..please advice me . 即时消息更新用户详细信息时出现错误..请告诉我。 im using laravel version 5 我正在使用laravel版本5

Error 错误

ModelNotFoundException in Builder.php line 125: No query results for model [App\User].

My controller file which i used auth/userController.php. 我使用auth / userController.php的控制器文件。

public function edit($id)
    {
        $User =  User::findOrFail($id);
//    return $User->username;
        return view('auth.editUser')->withUser($User);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  int $id
     * @return Response
     */
    public function update($id, Request $request)
    {
        $User = User::findOrFail($id);

        $this->validate($request, [
            'username' => 'required',
            'full_name' => 'required'
        ]);

        $input = $request->all();

        $User->fill($input)->save();

        Session::flash('flash_message', 'Task successfully added!');

        return redirect()->back();
    }

and below is my routes 以下是我的路线

Route:: get('user-edit/{id}', 'Admin\UserController@edit');
Route:: post('user-edit/{id}', 'Admin\UserController@update');

below is my edit.blade.php file for the view 下面是我的视图的edit.blade.php文件

<form class="form-horizontal" role="form" method="POST" action="{{ action('Admin\UserController@update') }}">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">

    <div class="form-group">
        <label class="col-md-4 control-label">Full Name</label>
        <div class="col-md-6">
            <input  type="text" class="form-control" name="full_name" value="{{ $user->full_name }}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-4 control-label">Username</label>
        <div class="col-md-6">
            <input  type="text" class="form-control" name="username" value="{{ $user->username }}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-4 control-label">Password</label>
        <div class="col-md-6">
            <input  type="password" class="form-control" name="password" value="{{ $user->password }}">
        </div>
    </div>



    <div class="form-group">
        <div class="col-md-6 col-md-offset-4">
            <button type="submit" class="btn btn-primary">
                Register
            </button>
        </div>
    </div>

</form>

The error is coming from your findOrFail() call. 该错误来自您的findOrFail()调用。 It is not finding a user with the id you're looking for. 找不到具有您要查找的ID的用户。

This may be due to the order of the parameters in your update method. 这可能是由于update方法中参数的顺序所致。 In the controller methods, the route parameters should be after the injected parameters. 在控制器方法中,路径参数应在注入参数之后。 The documentation states "If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies." 文档指出:“如果您的控制器方法也希望从route参数输入,则只需在其他依赖项之后列出您的route参数即可。” Try changing your update method to: 尝试将更新方法更改为:

public function update(Request $request, $id) {
    //
}

Edit 编辑

Your POST route requires the user's id as a route parameter, but you have not supplied this when generating the URL for the <form> . 您的POST路由需要用户的id作为路由参数,但是在生成<form>的URL时尚未提供此ID。 The second parameter of the action() function takes an array of values to use as the route parameters. action()函数的第二个参数采用值数组作为路由参数。 Your form statement should look like: 您的表单声明应如下所示:

<form class="form-horizontal" role="form" method="POST" action="{{ action('Admin\UserController@update', ['id' => $user->id]) }}">

The documentation for the action() helper function can be found here . 可以在此处找到action()辅助函数的文档。

Just pass a user id to your form like so: 只需将用户ID传递给您的表单即可,如下所示:

<form class="form-horizontal" role="form" method="POST" action="{{action('Admin\UserController@update', $user->id) }}">

You can also use form builder and do it like so: 您还可以使用表单构建器,如下所示:

{!! Form::open(['method' => 'POST', 'action' => ['UserController@update', $user->id], 'class' => 'form-horizontal']) !!}

if the whole action part is confusing you can do it this other way: 如果整个动作部分令人困惑,则可以通过其他方式进行操作:

<form class="form-horizontal" role="form" method="POST" action="{{url('user-edit/'.$user->id) }}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM