douwu8060 2016-10-12 16:28
浏览 183

在laravel 5.3模型中使用UUID

I've built a new Laravel 5.3 application, and in my tables I'm using postgres UUID as the primary and foreign keys.

In defining the Laravel models, I created the relationships normally. However, it appears that Laravel's ORM isn't translating the request properly:

PHP Warning:  Uncaught exception 'PDOException' with message 'SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for uuid: "0"'

I've searched the laravel docs but haven't found anything to explain how to use UUIDs as keys. Is there some additional configuration I need to do?

  • 写回答

1条回答 默认 最新

  • douyueqing1530 2016-12-13 12:50
    关注

    The reason for this is you need to tell the model that the key does not auto-increment.

    class User extends Authenticatable {
        use Notifiable, HasRole, SoftDeletes;
    
        public $incrementing = false;
    
        // User code
    

    Once you have set this, Eloquent then knows that it is not looking for an integer and will stop returning "0" when querying the model.

    You can follow a tutorial on using UUID's with laravel here: https://garrettstjohn.com/article/using-uuids-laravel-eloquent-orm/

    评论

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题