Home > Enterprise >  My migration isn't creating uuid column for my foreign key, I have also tried foreignUuid funct
My migration isn't creating uuid column for my foreign key, I have also tried foreignUuid funct

Time:09-21

I have attached my semester migration code and semester seeder code. Kindly tell me if you need any other pictures for guidance.

when I send data through seeders It only takes the numbers but not the string, because it creates an integer column in the database, Help me someone who knows about laravel migrations.

At the end, you can also see the database table of seemster.

[Semester Migration file][1]

Semester Seeder

Semester table in database

CodePudding user response:

From Docs:

The foreignIdFor method adds a {column}_id UNSIGNED BIGINT equivalent column for a given model class

Which basically means, it is creating BIGINT column in database not char like uuid.

It is better to use:

$table->foreignUuid('degreeId');

in migration.

CodePudding user response:

I would suggest revisiting your approach of using UUID. I believe you have missed some steps on your way while implementing UUID.

Add this to Semester Model

public $incrementing = false;

Create a trait

namespace App;
use Illuminate\Support\Str;
trait Uuids
{

    /**
     * Boot function from laravel.
     */
    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->{$model->getKeyName()} = Str::uuid()->toString();
        });
    }
}

Use this trait in the semester Model

Follow this brilliant article from medium and if the problem persists post it here again to ask your questions

https://medium.com/@steveazz/setting-up-uuids-in-laravel-5-552412db2088

  • Related