I am using Laravel 8 and trying to make database factories/seeders I am having some issues.
I have the following database Schema:
Schema::create('authors', function (Blueprint $table) { $table->id(); $table->string('first_name'); $table->string('last_name'); $table->timestamps(); }); Schema::create('books', function (Blueprint $table) { $table->char('isbn', 13)->unique('isbn'); $table->string('title'); $table->string('edition'); $table->date('release_date'); $table->string('image'); $table->timestamps(); }); //Create pivot table for author since its many to many relationship. Schema::create('author_book', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('author_id'); $table->char('book_id', 13); $table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade'); $table->foreign('book_id')->references('isbn')->on('books')->onDelete('cascade'); });
Now I am trying to create factories and database seeders but I get an error using the following line to seed:
Book::factory() ->has(Author::factory()->count(2)) ->create();
This gives me the following error:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`bokel`.`author_book`, CONSTRAINT `author_book_book_id_foreign` FOREIGN KEY (`book_id`) REFERENCES `books` (`isbn`) ON DELETE CASCADE) (SQL: insert into `author_book` (`author_id`, `book_id`) values (1, 0), (2, 0))
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.
Book::factory() ->has(Author::factory()->count(2), function (array $attributes, Book $book) { return ['book_id' => $book->isbn]; }) ->create();
The Factory definitions are the following:
public function definition() { return [ 'isbn' => $this->faker->unique()->isbn13, 'title' => $this->faker->sentence, 'edition' => $this->faker->numberBetween(1, 10), 'release_date' => $this->faker->date(), 'image' => $this->faker->imageUrl(), ]; } public function definition() { return [ 'first_name' => $this->faker->firstName, 'last_name' => $this->faker->lastName ]; }
Advertisement
Answer
As I mentioned in the comment I think your problem is relation in your Book
model.
In your Book
model you need:
public function authors() { return $this->belongsToMany(Author::class, 'author_book', 'book_id', 'author_id', 'isbn', 'id') ->using(AuthorBook::class); }
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!