Im在Laravel的用戶模塊中工作。帶有自定義「id」屬性的Laravel的'where子句'中的未知列'id'
我已經創建了名爲'id_user'的增量字段的表'用戶'。
我已經覆蓋了模型的$primaryKey
,但它繼續使用'id'默認字段進行查詢。
我也試過$key
,但它是一樣的。
我想在我的表'用戶'中通過'id'更改我的'id_user'字段可以工作,但我需要在我的表中使用自定義ID。
在這裏,我貼我的相關文件:
控制器: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)
{
//
}
}
型號: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;
}
}
遷移
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,
),
);
任何想法,我做錯了嗎?爲什麼它默認使用'id'而不是我自定義的'id_user'來查詢?
謝謝!
可能的重複:http://stackoverflow.com/questions/14468478/hard-coded-id-column-in-laravel-query-php – Digzol