在 Laravel 6.5.1 上更改 ID 列名称
Change id column name on Laravel 6.5.1
我在 Laravel 6.5.1 中有一个模型用户。
class User extends Authenticatable
{
use Notifiable;
public $table = 'usuario';
public $primaryKey = 'cif_usu';
public $incrementing = false;
public $timestamp = false;
//...
}
但是,当我尝试 select 一个用户时,出现以下错误:
SQLSTATE[42703]: Undefined column: 7 ERROR: column "id" does not exist
LINE 1: select * from "usuario" where "id" = limit 1 ^ (SQL: select
* from "usuario" where "id" = 24 limit 1)
如何重命名 id 列?
编辑:
我改了:
public $primaryKey = 'cif_usu';
至:
protected $primaryKey = 'cif_usu';
结果是一样的
$primaryKey
属性的可见性应为 protected
。我认为如果流量没有变化,您实际上并没有覆盖基本模型 class.
中的主键
如果不是这种情况,查看触发此查询的代码可能会有用
Eloquent will also assume that each table has a primary key column named id
. You may define a protected $primaryKey
property to override this convention:
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'key';
阅读更多关于 Laravel - Primary Keys
将主键和 table 设为受保护。
protected $table = 'usuario';
protected $primaryKey = 'cif_usu';
public $incrementing = false; //only if your primary key is an assignable field,not auto incremented
您可以将模型用作
DB::table('usuario')->where('cif_usu',$cif_usu)->first();
或
DB::table('usuario')->find($cif_usu);
我在 Laravel 6.5.1 中有一个模型用户。
class User extends Authenticatable
{
use Notifiable;
public $table = 'usuario';
public $primaryKey = 'cif_usu';
public $incrementing = false;
public $timestamp = false;
//...
}
但是,当我尝试 select 一个用户时,出现以下错误:
SQLSTATE[42703]: Undefined column: 7 ERROR: column "id" does not exist LINE 1: select * from "usuario" where "id" = limit 1 ^ (SQL: select * from "usuario" where "id" = 24 limit 1)
如何重命名 id 列?
编辑:
我改了:
public $primaryKey = 'cif_usu';
至:
protected $primaryKey = 'cif_usu';
结果是一样的
$primaryKey
属性的可见性应为 protected
。我认为如果流量没有变化,您实际上并没有覆盖基本模型 class.
如果不是这种情况,查看触发此查询的代码可能会有用
Eloquent will also assume that each table has a primary key column named
id
. You may define a protected$primaryKey
property to override this convention:
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'key';
阅读更多关于 Laravel - Primary Keys
将主键和 table 设为受保护。
protected $table = 'usuario';
protected $primaryKey = 'cif_usu';
public $incrementing = false; //only if your primary key is an assignable field,not auto incremented
您可以将模型用作
DB::table('usuario')->where('cif_usu',$cif_usu)->first();
或
DB::table('usuario')->find($cif_usu);