Skip to content
Advertisement

Laravel Many-to-Many Factory with custom id type

I am using Laravel 8 and trying to make database factories/seeders I am having some issues.

I have the following database Schema:

JavaScript

Now I am trying to create factories and database seeders but I get an error using the following line to seed:

JavaScript

This gives me the following error:

JavaScript

Which is if I understand it correctly because an isbn number is excpected but laravel just puts an integer into the pivot table. What would be the best way to solve this? I was thinking something like this but that doesn’t work either.

JavaScript

The Factory definitions are the following:

JavaScript

Advertisement

Answer

As I mentioned in the comment I think your problem is relation in your Book model.

In your Book model you need:

JavaScript

Try like that and if this doesn’t solve your problem I’ll delete the answer.

Take a look in the docs, and check link has many relationships in Many to Many section.

Good luck!

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