2

I have just started learning laravel. I'm familiar with CakePHP.

I used to use UUID field as primary key in my database and in CakePHP, it was quite simple to just change data type of column field to CHAR(36) and it works well.

In Laravel, I have modified users migration to change increments to uuid field and set to primary key

CreateUserTable

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->uuid('id');
            $table->primary('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

        Schema::table('users', function (Blueprint $table) {
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->uuid('role_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

But, when I save a new record, it gives error as

Illuminate\Database\QueryException thrown with message
"SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value 
(SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) 
values (Anuj, anuj@example.com, password_hash, date-time, date-time))"
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • laraval has it's syntax u need to respect that as u respect the cakephp syntax – Thamer Jan 19 '18 at 21:07
  • @Thamerbelfkih This code is perfectly acceptable for Laravel, but there is some validity to that comment. What's the `User` model look like? What does the code for saving this User look like? We would need those in order to debug this further. – Tim Lewis Jan 19 '18 at 21:11
  • @TimLewis I'm using default registration page to create an account for user. – Anuj TBE Jan 19 '18 at 21:17

2 Answers2

4

You need to generate the UUID as well - unlike auto incrementing unsigned integer keys, the UUID field doesn't just populate itself.

One popular and very simple-to-use such package is alsofronie/eloquent-uuid, which is available here: https://github.com/alsofronie/eloquent-uuid

Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
  • So, I need to create this manually and `Laravel` does not create it on it's own like `CakePHP`? – Anuj TBE Jan 19 '18 at 21:22
  • I installed this plugin. But again it says same error. Do I need to make any further modification to `Model` or migration file? – Anuj TBE Jan 19 '18 at 21:28
  • No, It has nothing more than installing and changing field type to `uuid`, which I have already done in the migration. – Anuj TBE Jan 19 '18 at 21:31
  • The instructions say "In order to use this in your models, just put use Uuid[32|Binary]ModelTrait;:" and a code example. – Joel Hinz Jan 19 '18 at 21:35
  • Yes, I tried that too. But, in example `User` model extends `Eloquent` while default user model in my app extends `Authenticable`. Further more, adding `Uuid[32|Binary]ModelTrait;` is giving syntaxt error `Parse error: syntax error, unexpected '[', expecting ',' or ';' or '{'`. – Anuj TBE Jan 19 '18 at 21:38
2

What I did for this one is to create a Trait for Models that uses UUID:

trait WithUuid
{
    public static function boot()
    {
        parent::boot();

        self::creating(function ($model) {
            $model->{$model->getKeyName()} = (string) Uuid::generate(4);
        });
    }

    public function initializeHasUuid()
    {
        $this->incrementing = false;
        $this->keyType = 'string';
    }
}

then on your Model just declare that Trait:


class User extends Model
{
    use WithUuid;

   // rest of your Code
}

So you don't have to create a new one everytime you create something.

I am L
  • 4,288
  • 6
  • 32
  • 49