php Laravel 5.2 - 使用字符串作为 Eloquent Table 的自定义主键变为 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34582535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:05:50  来源:igfitidea点击:

Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table becomes 0

phpmysqllaravellaravel-5eloquent

提问by Abrar Jahin

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-

我的数据库是这样的-

enter image description here

在此处输入图片说明

but if I do this-

但如果我这样做——

UserVerification::where('verification_token', $token)->first();

I am getting this-

我得到这个-

{
  "email": "[email protected]",
  "verification_token": 0,
  "created_at": "2016-01-03 22:27:44",
  "updated_at": "2016-01-03 22:27:44"
}

So, the verification token/primary keybecomes 0.

因此,验证令牌/主键变为 0。

Can anyone please help?

有人可以帮忙吗?

回答by andrewtweber

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;

    // In Laravel 6.0+ make sure to also set $keyType
    protected $keyType = 'string';
}

回答by lagbox

On the model set $incrementingto false

在模型上设置$incrementing为 false

public $incrementing = false;

This will stop it from thinking it is an auto increment field.

这将阻止它认为它是一个自动增量字段。

Laravel Docs - Eloquent - Defining Models

Laravel Docs - Eloquent - 定义模型

回答by Wader

Theres two properties on the model you need to set. The first $primaryKeyto tell the model what column to expect the primary key on. The second $incrementingso it knows the primary key isn't a linear auto incrementing value.

您需要设置模型上的两个属性。第一个$primaryKey告诉模型期望主键位于哪个列。第二个$incrementing所以它知道主键不是线性自动递增值。

class MyModel extends Model
{
    protected $primaryKey = 'my_column';

    public $incrementing = false;
}

For more info see the Primary Keyssection in the documentation on Eloquent.

有关更多信息,请参阅Eloquent 文档中Primary Keys部分。

回答by Erich Meissner

I was using Postman to test my Laravel API.

我使用 Postman 来测试我的 Laravel API。

I received an error that stated

我收到一个错误,指出

"SQLSTATE[42S22]: Column not found: 1054 Unknown column" because Laravel was trying to automatically create two columns "created_at" and "updated_at".

“SQLSTATE[42S22]: Column not found: 1054 Unknown column”因为 Laravel 试图自动创建两列“created_at”和“updated_at”。

I had to enter public $timestamps = false;to my model. Then, I tested again with Postman and saw that an "id" = 0variable was being created in my database.

我必须进入public $timestamps = false;我的模型。然后,我再次使用 Postman 进行测试,发现"id" = 0在我的数据库中创建了一个变量。

I finally had to add public $incrementing false;to fix my API.

我终于不得不添加public $incrementing false;来修复我的 API。

回答by Zμ 1

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);