Laravel 5.2-将主键设置为电子邮件而不是'id' [英] Laravel 5.2 - set Primary key to email instead of 'id'

查看:53
本文介绍了Laravel 5.2-将主键设置为电子邮件而不是'id'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Schema::create('users', function (Blueprint $table) {<br>
            $table->primary('email');<br>
            $table->string('name');<br>
            $table->string('image_url');<br>
            $table->string('signin_with');<br>
        });

以上是我的数据库架构.

Above is my Database Schema.

这是我的用户模型.

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Users extends Model
{

    protected $table = 'users';
    public $incrementing = false;
    protected $primaryKey = 'email';

    protected $fillable = [
        'email', 'name' ,'image_url', 'signin_with'
    ];
} 

现在我的问题是我想使'email'属性成为用户表中的主键.但我得到

Now my question is I want to make 'email' attribute as primary key in users table. but i get an error of

QueryException SQLState [42S01].

QueryException SQLState[42S01].

我该如何解决?

先谢谢您.

推荐答案

主键用于链接到其他表,并且字符串比较比int比较慢.

Primary key is used for linking to other tables and string comparison is slower than int comparison.

如果将用户信息存储在多个表中,则用户表的外键将是电子邮件地址.

If you store users information in multiple tables, the foreign keys to the users table will be the e-mail address.

这意味着您多次存储了电子邮件地址.因此,不要使用电子邮件作为主键,而应使用自动递增的主键.

That means that you store the e-mail address multiple times. So, instead of using email as primary key use an auto-incremented primary key.

这篇关于Laravel 5.2-将主键设置为电子邮件而不是'id'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆