Using a UUID as primary key with Laravel 5

后端 未结 4 1518
余生分开走
余生分开走 2021-01-03 07:27

I\'m trying to create tables that will have a primary key which is a UUID defined as binary(16) instead of the default auto-incrementing id field.<

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 07:56

    This is a quick solution without using events.

    UUidModel.php

    getKeyName();
                $id = $attributes[$keyName] = $this->generateNewId();
                $query->insert($attributes);
                $this->setAttribute($keyName, $id);
            }
    
        /**
        * Get a new version 4 (random) UUID.
        */
            public function generateNewId()
            {
                return 'uuid!!' ;// just for test
            }
        }
    ?>
    

    Model Example Car.php

提交回复
热议问题