Laravel 具有自定义 ID 类型的多对多工厂
Laravel Many-to-Many Factory with custom id type
我正在使用 Laravel 8 并尝试创建数据库 factories/seeders 我遇到了一些问题。
我有以下数据库模式:
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');
});
现在我正在尝试创建工厂和数据库播种器,但使用以下行播种时出现错误:
Book::factory()
->has(Author::factory()->count(2))
->create();
这给了我以下错误:
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))
如果我理解正确的话,这是因为 isbn 编号是预期的,但 laravel 只是将一个整数放入主元 table。解决这个问题的最佳方法是什么?我在想这样的事情,但那也不管用。
Book::factory()
->has(Author::factory()->count(2), function (array $attributes, Book $book) {
return ['book_id' => $book->isbn];
})
->create();
工厂定义如下:
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
];
}
正如我在评论中提到的,我认为你的问题是你的 Book
模型中的关系。
在您的 Book
模型中,您需要:
public function authors()
{
return $this->belongsToMany(Author::class,
'author_book', 'book_id', 'author_id', 'isbn', 'id')
->using(AuthorBook::class);
}
试试看,如果这不能解决您的问题,我会删除答案。
查看多对多部分的docs, and check link has many relationships。
祝你好运!
我正在使用 Laravel 8 并尝试创建数据库 factories/seeders 我遇到了一些问题。
我有以下数据库模式:
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');
});
现在我正在尝试创建工厂和数据库播种器,但使用以下行播种时出现错误:
Book::factory()
->has(Author::factory()->count(2))
->create();
这给了我以下错误:
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))
如果我理解正确的话,这是因为 isbn 编号是预期的,但 laravel 只是将一个整数放入主元 table。解决这个问题的最佳方法是什么?我在想这样的事情,但那也不管用。
Book::factory()
->has(Author::factory()->count(2), function (array $attributes, Book $book) {
return ['book_id' => $book->isbn];
})
->create();
工厂定义如下:
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
];
}
正如我在评论中提到的,我认为你的问题是你的 Book
模型中的关系。
在您的 Book
模型中,您需要:
public function authors()
{
return $this->belongsToMany(Author::class,
'author_book', 'book_id', 'author_id', 'isbn', 'id')
->using(AuthorBook::class);
}
试试看,如果这不能解决您的问题,我会删除答案。
查看多对多部分的docs, and check link has many relationships。
祝你好运!