未知列' id'在' where子句'在Laravel中使用自定义' id'属性

时间:2014-03-31 17:27:40

标签: php mysql laravel eloquent

我在Laravel的用户模块中工作。

我使用名为“id_user”的增量字段创建了表'users'。

我已覆盖模型的$primaryKey,但它继续使用'id'默认字段进行查询。

我也试过了$key,但它也一样。

我想在我的表'users'中用'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'?

谢谢!

2 个答案:

答案 0 :(得分:1)

我遇到了Validator类的问题。它没有使用我指定的$ primaryKey,因为电子邮件的规则是错误的。

这是正确的:

$rules['email'] .= ',' . $this->id_user . ',id_user'; 

其中第三个参数(id_user)是用于进行查询的主键。

答案 1 :(得分:0)

错误似乎出现在store方法中。

更改

return Redirect::route('admin.users.show', array($user->id));

return Redirect::route('admin.users.show', array($user->id_user));