I am trying to use email as my table's primary key, so my eloquent code is-
我正在尝试使用电子邮件作为我的表的主键,所以我雄辩的代码是-
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserVerification extends Model
{
protected $table = 'user_verification';
protected $fillable = [
'email',
'verification_token'
];
//$timestamps = false;
protected $primaryKey = 'verification_token';
}
And my DB is like this-
DB是这样的
but if I do this-
但是如果我这样做-
UserVerification::where('verification_token', $token)->first();
I am getting this-
我得到这个
{
"email": "sdfsdf@sdfsdf.sdf",
"verification_token": 0,
"created_at": "2016-01-03 22:27:44",
"updated_at": "2016-01-03 22:27:44"
}
So, the verification token/primary key becomes 0.
因此,验证令牌/主键变为0。
Can anyone please help?
谁能请帮助?
4 个解决方案
#1
95
This was added to the upgrade documentation on Dec 29, 2015, so if you upgraded before then you probably missed it.
这是在2015年12月29日添加到升级文档中的,所以如果您在升级之前就升级了,那么您可能会错过它。
When fetching any attribute from the model it checks if that column should be cast as an integer, string, etc.
当从模型中获取任何属性时,它检查该列是否应该被转换为整数、字符串等。
By default, for auto-incrementing tables, the ID is assumed to be an integer in this method:
默认情况下,对于自动递增的表,ID被假定为这个方法中的整数:
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L2790
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php L2790
So the solution is:
所以解决方法是:
class UserVerification extends Model
{
protected $primaryKey = 'your_key_name'; // or null
public $incrementing = false;
}
#2
16
On the model set $incrementing
to false
在模型中,将$increment给false
public $incrementing = false;
This will stop it from thinking it is an auto increment field.
这将阻止它认为它是一个自动增量字段。
Laravel Docs - Eloquent - Defining Models
Laravel博士——有说服力的模型。
#3
2
Theres two properties on the model you need to set. The first $primaryKey
to tell the model what column to expect the primary key on. The second $incrementing
so it knows the primary key isn't a linear auto incrementing value.
您需要在模型上设置两个属性。第二个$increment给它知道主键不是一个线性自动递增值。
class MyModel extends Model
{
protected $primaryKey = 'my_column';
public $incrementing = false;
}
For more info see the Primary Keys
section in the documentation on Eloquent.
有关更多信息,请参阅有关专业术语的文档中的主键部分。
#4
0
keep using the id
继续使用的id
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserVerification extends Model
{
protected $table = 'user_verification';
protected $fillable = [
'id',
'email',
'verification_token'
];
//$timestamps = false;
protected $primaryKey = 'verification_token';
}
and get the email :
收到邮件:
$usr = User::find($id);
$token = $usr->verification_token;
$email = UserVerification::find($token);
#1
95
This was added to the upgrade documentation on Dec 29, 2015, so if you upgraded before then you probably missed it.
这是在2015年12月29日添加到升级文档中的,所以如果您在升级之前就升级了,那么您可能会错过它。
When fetching any attribute from the model it checks if that column should be cast as an integer, string, etc.
当从模型中获取任何属性时,它检查该列是否应该被转换为整数、字符串等。
By default, for auto-incrementing tables, the ID is assumed to be an integer in this method:
默认情况下,对于自动递增的表,ID被假定为这个方法中的整数:
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L2790
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php L2790
So the solution is:
所以解决方法是:
class UserVerification extends Model
{
protected $primaryKey = 'your_key_name'; // or null
public $incrementing = false;
}
#2
16
On the model set $incrementing
to false
在模型中,将$increment给false
public $incrementing = false;
This will stop it from thinking it is an auto increment field.
这将阻止它认为它是一个自动增量字段。
Laravel Docs - Eloquent - Defining Models
Laravel博士——有说服力的模型。
#3
2
Theres two properties on the model you need to set. The first $primaryKey
to tell the model what column to expect the primary key on. The second $incrementing
so it knows the primary key isn't a linear auto incrementing value.
您需要在模型上设置两个属性。第二个$increment给它知道主键不是一个线性自动递增值。
class MyModel extends Model
{
protected $primaryKey = 'my_column';
public $incrementing = false;
}
For more info see the Primary Keys
section in the documentation on Eloquent.
有关更多信息,请参阅有关专业术语的文档中的主键部分。
#4
0
keep using the id
继续使用的id
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserVerification extends Model
{
protected $table = 'user_verification';
protected $fillable = [
'id',
'email',
'verification_token'
];
//$timestamps = false;
protected $primaryKey = 'verification_token';
}
and get the email :
收到邮件:
$usr = User::find($id);
$token = $usr->verification_token;
$email = UserVerification::find($token);