faz faz
faz faz

Reputation: 171

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

Error

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

My controller file which i used 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

<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>

Upvotes: 1

Views: 596

Answers (2)

user5029208
user5029208

Reputation:

Just pass a user id to your form like so:

<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) }}">

Upvotes: 1

patricus
patricus

Reputation: 62228

The error is coming from your findOrFail() call. It is not finding a user with the id you're looking for.

This may be due to the order of the parameters in your update method. 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." 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>. The second parameter of the action() function takes an array of values to use as the route parameters. 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.

Upvotes: 1

Related Questions