首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将字符串设置为主键而不是laravel-9 & php中的默认id?

如何将字符串设置为主键而不是laravel-9 & php中的默认id?
EN

Stack Overflow用户
提问于 2022-09-07 07:17:07
回答 1查看 238关注 0票数 -1

这是我的迁移表模式

代码语言:javascript
复制
    public function up()
    {
        Schema::create('ip_users', function (Blueprint $table) {
            $table->id('user_id',10);
            $table->string('user_uuid_id',60);
            $table->string('user_name',30);
            $table->string('user_email',30);
            $table->string('user_password',100);
            $table->boolean('user_active');
            $table->timestamp('user_date_created')->useCurrent();
            $table->timestamp('user_date_modified')->useCurrent();
            $table->comment("Registered user's credentials");
        });
    }

下面是我对迁移表的对应模型

代码语言:javascript
复制
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class IPUsers extends Model
{
    use HasFactory;

    /* Our own defined primay key */
    protected $table = 'ip_users';
    public $incrementing = false;
    protected $primaryKey = 'user_PK_id'; 

    /* To tell the eloquent model that our PrimaryKey type is not an integer*/
    protected $keyType = 'string';
}

当我执行php artisan migrate时,模式被存储为'user_id‘作为主键,但我希望'user_uuid_id’成为主键。为什么迁移在迁移之前不读取“IPUsers”模型,或者迁移不是那样工作的?

如果模型有另一个目的,它是什么?

如果迁移不是那样工作的,那么如何将PK设置为我自己的列而不是默认的id?

我在laracasts中读到,laravel没有实现自定义的PK。解决这个问题的办法是什么?

EN

回答 1

Stack Overflow用户

发布于 2022-09-07 07:21:58

只需向uuid字段添加primary()。

代码语言:javascript
复制
$table->string('user_uuid_id',60)->primary();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73631469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档