acharmat
acharmat

Reputation: 125

Error edit() must be an instance of App\User, none given

i created a UserController.php

use App\User;
//

class UsersController extends Controller
{
//

    public function edit($id, User $user){
      $user = $user->find($id);
      return view('admin.user.edit' , compact('user'));
    }

 //


}

And edit views

but when i want to access to this url

url('/users/'. $user->id .'edit')

i got this error

Error edit() must be an instance of App\User, none given

!?

Upvotes: 2

Views: 709

Answers (2)

Amit Gupta
Amit Gupta

Reputation: 17658

Make sure your route is like:

Route::get(/users/{id}/edit', 'UsersController@edit');

Then use it as:

url('/users/'. $user->id .'/edit');

or

url("/users/{$user->id}/edit");

And then your controller should be as:

public function edit($id) {
  $user = User::find($id);
  return view('admin.user.edit' , compact('user'));
}

Upvotes: 2

Can Celik
Can Celik

Reputation: 2087

The error says that the edit() function expects $user_id and User instance but you're only providing $user_id. If that's the case your code should look like this:

public function edit($id){
      $user = User::find($id);
      return view('admin.user.edit' , compact('user'));
}

Upvotes: 1

Related Questions