Laravel primaryKey override Ignored - 查询返回未知列错误
Laravel primaryKey override Ignored - Query returning unknown column error
我正在使用一个数据库,其中 id 列名为 user_id。
我已经在用户模型中设置了 $primaryKey 覆盖,但它仍在寻找 id
。它出现在用户模型扩展 Authenticatable 时,它被忽略了。 Authenticatable extends modal,所以应该不是问题,对吧?
我已经看到很多主键覆盖不起作用的线程,但没有任何帮助。
访问用户时出错:
Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from `members` where `email` = user@gmail.com and `id` <> 1804)
用户模型:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'members';
protected $primaryKey = 'user_id';
更新产生错误:
public function rules()
{
$user = Auth::user();
return [
'email' => [
'required',
'email',
Rule::unique($user->getTable())->ignore($user->getKey()),
],
'first_name' => 'required',
'last_name' => 'required',
];
}
数据库列:
Name | Type | Attributes | Null | Extra
user_id (PrimaryKey) | int(60) | Unsigned | No | AUTO_INCREMENT
sql 语句看起来像是唯一验证规则会生成的内容。如果主键不是 id
.
,它具有指定主键的语法
If your table uses a primary key column name other than id, you may specify the name of the column when calling the ignore method:
'email' => Rule::unique('users')->ignore($user->id, 'user_id')
来源:https://laravel.com/docs/5.5/validation#rule-unique,第"Forcing A Unique Rule To Ignore A Given ID".
段下
我正在使用一个数据库,其中 id 列名为 user_id。
我已经在用户模型中设置了 $primaryKey 覆盖,但它仍在寻找 id
。它出现在用户模型扩展 Authenticatable 时,它被忽略了。 Authenticatable extends modal,所以应该不是问题,对吧?
我已经看到很多主键覆盖不起作用的线程,但没有任何帮助。
访问用户时出错:
Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from `members` where `email` = user@gmail.com and `id` <> 1804)
用户模型:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'members';
protected $primaryKey = 'user_id';
更新产生错误:
public function rules()
{
$user = Auth::user();
return [
'email' => [
'required',
'email',
Rule::unique($user->getTable())->ignore($user->getKey()),
],
'first_name' => 'required',
'last_name' => 'required',
];
}
数据库列:
Name | Type | Attributes | Null | Extra
user_id (PrimaryKey) | int(60) | Unsigned | No | AUTO_INCREMENT
sql 语句看起来像是唯一验证规则会生成的内容。如果主键不是 id
.
If your table uses a primary key column name other than id, you may specify the name of the column when calling the ignore method:
'email' => Rule::unique('users')->ignore($user->id, 'user_id')
来源:https://laravel.com/docs/5.5/validation#rule-unique,第"Forcing A Unique Rule To Ignore A Given ID".
段下