【问题标题】:Unknown column 'id' in 'where clause' in Laravel with custom 'id' attributeLaravel 中“where 子句”中的未知列“id”,带有自定义“id”属性
【发布时间】:2014-03-31 17:27:39
【问题描述】:

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

我创建了表“users”,其中包含一个名为“id_user”的增量字段。

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

我也试过$key,但还是一样。

我想在“users”表中将“id_user”字段更改为“id”会起作用,但我需要在表中使用自定义 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'?

谢谢!

【问题讨论】:

标签: php mysql laravel eloquent


【解决方案1】:

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

这是正确的:

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

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

【讨论】:

    【解决方案2】:

    错误似乎在store 方法中。

    改变

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

    收件人

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

    【讨论】:

    • 我的错,这是一个粘贴错误。我的代码中有 $user->id_user ,但它仍然按 'id' 而不是 id_user 搜索:\
    猜你喜欢
    • 1970-01-01
    • 2016-05-20
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多