Skip to content
Advertisement

MySQL type CHAR problem in Laravel Eloquent

They say, if you want to find the answer, ask the right question. Really, here I don’t know what to ask. Because I don’t know what’s happening.

Migration:create_table1_table

JavaScript

I seeded the database and the following is a sample:

JavaScript

In tinker:

JavaScript

returns:

JavaScript

Here where I cannot understand:

JavaScript

This causes my model which has a hasMany(Table2::class, ‘t1_pk’) relationship with table2 to not work correctly:

JavaScript

Then:

JavaScript

Returns:

JavaScript

What so far I understood column ‘code’ type juggles to integer:

JavaScript

What is going on here and how to make laravel’s Eloquent hasMany() to generate a correct query?

Thanks in advance.

Advertisement

Answer

You have to cast your custom primary key to the correct type and ensure that Eloquent doesn’t think that your primary key is auto incrementable:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement