Hola!
Estoy haciendo un modulo de usuarios utilizando el framework Laravel de php.
Mi base de datos tiene una tabla 'users' con una clave primaria autoincrementada llamada 'id_user'. Laravel por defecto utiliza 'id' como clave primaria para los métodos del orm, y para asignar otra clave primaria hay que sobrescribir la variable $primaryKey en el modelo a utilizar.
Pues bien, yo he sobreescrito esa variable en el modelo 'user' pero laravel me sigue tirando al editar un usuario con:
Illuminate \ Database \ QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from `users` where `email` = [email protected] and `id` <> 1)
Sabéis por que puede ser? Os dejo por aqui el código implicado:
UserController.php<?php
class Admin_UsersController extends \BaseController {
public function index()
{
$users = User::paginate();
return View::make('admin/users/list')->with('users', $users);
}
public function create()
{
$user = new User;
return View::make('admin/users/form')->with('user',$user);;
}
public function store()
{
$user = new User;
$data = Input::all();
if ($user->isValid($data))
{
$user->fill($data);
$user->save();
return Redirect::route('admin.users.show', array($user->id_user));
}
else
{
return Redirect::route('admin.users.create')->withInput()->withErrors($user->errors);
}
}
public function show($id)
{
$user = User::find($id);
if (is_null($user))
{
App::abort(404);
}
else
{
return View::make('admin/users/profile')->with('user',$user);
}
}
public function edit($id)
{
$user = User::find($id);
if (is_null ($user))
{
App::abort(404);
}
return View::make('admin/users/form')->with('user', $user);
}
public function update($id)
{
$user = User::find($id);
if (is_null ($user))
{
App::abort(404);
}
$data = Input::all();
if ($user->isValid($data))
{
$user->fill($data);
$user->save();
return Redirect::route('admin.users.show', array($user->id_user));
}
else
{
return Redirect::route('admin.users.edit', $user->id_user)->withInput()->withErrors($user->errors);
}
}
public function destroy($id)
{
//
}
}
User.php<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
public $errors;
protected $fillable = array('user','email', 'password','fullname','address','rank');
protected $primaryKey = 'id_user';
protected $perPage = 2;
protected $table = 'users';
protected $hidden = array('password');
public function getAuthIdentifier()
{
return $this->getKey();
}
public function getAuthPassword()
{
return $this->password;
}
public function getReminderEmail()
{
return $this->email;
}
public function isValid($data)
{
$rules = array(
'user' => 'required|min:4|max:15|unique:users',
'password' => 'min:7|confirmed',
'email' => 'required|email|unique:users,email',
'fullname' => 'required|min:4|max:40',
'address' => 'required|min:8',
'rank' => 'required'
);
if ($this->exists)
{
$rules['email'] .= ',' . $this->id_user;
}
else
{
$rules['password'] .= '|required';
}
$validator = Validator::make($data, $rules);
if ($validator->passes())
{
return true;
}
$this->errors = $validator->errors();
return false;
}
}