Hi, when I tried to access this http://localhost:8000/user/3/edit url in my app, I'm getting this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id' in 'where clause' (SQL: select * from users
where users
.id
= 2 limit 1)
this is my edit function on my UserController.php file
public function edit($user_id) { // get the user $user = User::find($user_id); //dd(DB::getQueryLog()); //$user = DB::table('users')->where('user_id', $user_id)->first();
// show the edit form and pass the user
return View::make('user.edit')->with('user', $user);
}
this is my routes.php Route::resource('user', 'UserController');
and this is my edit.blade.php file:
<!-- app/views/user/edit.blade.php --> <!DOCTYPE html> <html> <head> <title>Look! I'm CRUDding</title> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"> </head> <body> <div class="container"> <nav class="navbar navbar-inverse"> <div class="navbar-header"> <a class="navbar-brand" href="{{ URL::to('user') }}">User Alert</a> </div> <ul class="nav navbar-nav"> <li><a href="{{ URL::to('user') }}">View All Users</a></li> <li><a href="{{ URL::to('user/create') }}">Create a User</a> </ul> </nav> <h1>Edit {{ $user->user_name }}</h1> <!-- if there are creation errors, they will show here -->{{ HTML::ul($errors->all()) }}
{{ Form::model($user, array('action' => array('UserController@update', $user->user_id), 'method' => 'PUT')) }}
<div class="form-group">
{{ Form::label('user_name', 'Name') }}
{{ Form::text('user_name', null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('user_email', 'Email') }}
{{ Form::email('user_email', null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('user_password', 'Password') }}
{{ Form::text('user_password', null, array('class' => 'form-control')) }}
</div>
{{ Form::submit('Edit the User!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div> </body> </html>Can you help me please?
Tempory comment out this line
return View::make('user.edit')->with('user', $user);
And do a
print_r($user);
That will let you know if it's successfully getting through the query.
If no results here troubleshoot if $user_id is being passed to the method.
You could comment out everything in method and
echo $user_id;
If that echoes and the print_r shows a results, then double check your query.
Also here is a youtube video on editing a database record in laravel.
https://www.youtube.com/watch?v=q0t6JIY707A
This may sound like a silly question, but is the name of your table user
or users
? That would be why the database query is failing (SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id' in 'where clause' (SQL: select * from users where users.id = 2 limit 1)
), what if you change your query to:
DB::table('user')->where('user_id', '=', $user_id)->first();
What does your User model look like, do you set the protected $table;
property?
Thank you very much for the answers. I found the problem, my primary ky is user_id, and laravel by default uses id for all tables Ids, so I added this line to my user model and that works perfect.
protected $primaryKey = 'user_id';
Thank you. cheers
you guys are a life saver. The last solution worked as a magic.... Thank you so much juanpscotto...
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community