Reputation: 99
Im working in a users module in Laravel.
I have created the table 'users' with an incremental field called 'id_user'.
I have overridden the $primaryKey
for the model but it keep doing the query with 'id' default field.
I have tried with $key
too, but it's the same.
I suppose that changing my 'id_user' field by 'id' in my table 'users' will work, but I need to use custom ids in my tables.
Here I paste my relevant files:
Controller: UsersController.php
class Admin_UsersController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
//GET llama a index, POST llama a store
public function index()
{
$users = User::paginate();
//dd($users); //dump de $users
return View::make('admin/users/list')->with('users', $users);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$user = new User;
return View::make('admin/users/form')->with('user', $user);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
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));
}else{
return Redirect::route('admin.users.create')->withInput()->withErrors($user->errors);
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$user = User::find($id);
if (is_null($user))
{
App::abort(404);
}
return View::make('admin/users/profile')->with('user', $user);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$user = User::find($id);
if (is_null ($user))
{
App::abort(404);
}
return View::make('admin/users/form')->with('user', $user);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
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);
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
Model: User.php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
public $primaryKey='id_user';
public $table = 'users';
public $errors;
protected $fillable = array('email', 'fullname', 'password','user', 'address', 'rank');
protected $perPage = 2;
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
public function isValid($data)
{
$rules = array(
'user' => 'required|min:4|max:15|unique:users',
'password' => 'required|min:7|confirmed',
'email' => 'required|email|unique:users',
'fullname' => 'required|min:4|max:40',
'address' => 'required|min:8',
'rank' => 'required'
);
// Si el usuario existe:
if ($this->exists)
{
$rules['email'] .= ',email,' . $this->id_user;
$rules['user'] .= ',user,' . $this->id_user;
}
else
{
$rules['password'] .= '|required';
}
$validator = Validator::make($data, $rules);
if ($validator->passes())
{
return true;
}
$this->errors = $validator->errors();
return false;
}
}
Migration
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id_user');
$table->string('user');
$table->string('password');
$table->string('email');
$table->string('fullname');
$table->string('address');
$table->string('rank');
$table->string('avatar');
$table->timestamps();
});
}
Auth.php
return array(
/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/
'driver' => 'eloquent',
/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/
'model' => 'User',
/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/
'table' => 'users',
/*
|--------------------------------------------------------------------------
| Password Reminder Settings
|--------------------------------------------------------------------------
|
| Here you may set the settings for password reminders, including a view
| that should be used as your password reminder e-mail. You will also
| be able to set the name of the table that holds the reset tokens.
|
| The "expire" time is the number of minutes that the reminder should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
Any idea what i'm doing wrong? Why it keeps quering by default 'id' instead my custom 'id_user'?
Thanks!
Upvotes: 3
Views: 5860
Reputation: 99
I had a problem with Validator class. It didn't use the $primaryKey that i have assigned because the email's rule was wrong.
This is the correct one:
$rules['email'] .= ',' . $this->id_user . ',id_user';
Where 3rd argument (id_user) is the primary key to use in order to make the query.
Upvotes: 1
Reputation: 23992
Error seems to be in the store
method.
Change:
return Redirect::route('admin.users.show', array($user->id));
To:
return Redirect::route('admin.users.show', array($user->id_user));
Upvotes: 0