データベース/移行/2018_12_20_022430_create_products_table.php
> class CreateProductsTable extends Migration {
> /**
> * Run the migrations.
> *
> * @return void
> */
> public function up()
> {
> Schema::create('products', function (Blueprint $table) {
> $table->increments('id');
> $table->string ('name');
> $table->text('description');
> $table->decimal('price');
> $table->string('file');
> $table->timestamps();
> });
> }
データベース/移行/2018_12_20_042857_create_cards_table.php
class CreateCardsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cards', function (Blueprint $table) {
$table->increments('id');
$table->string ('quantity');
$table->string('status');
$table->integer('pid')->unsigned();
$table->foreign('pid')->references('id')->on('products');
$table->integer('cid')->unsigned();
$table->foreign('cid')->references('id')->on('customers');
$table->timestamps();
});
}
database / factorys / UserFactory.php
$factory->define(App\Product::class, function (Faker $faker) {
return [
//'id'=>$faker->numberBetween($min = 1, $max = 20),
'name'=> $faker->word,
'description'=> $faker->sentence($nbWords = 6, $variableNbWords = true),
'price' => $faker->randomFloat($nbMaxDecimals = 100, $min = 1, $max = 10),
'file' => $faker->imageUrl($width = 640, $height = 480),
];
});
$factory->define(App\Card::class, function (Faker $faker) {
return [
//'id'=>$faker->numberBetween($min = 1, $max = 20),
'quantity'=>$faker->sentence($nbWords = 6, $variableNbWords = true),
'status'=>$faker->boolean($chanceOfGettingTrue = 10),
'cid'=>$faker->numberBetween($min = 1, $max = 20),
'pid'=>$faker->numberBetween($min = 1, $max = 20),
];
});
ルート/web.php
factory(App\Product::class,5)->create();
factory(App\Card::class,5)->create();
ターミナル:
$ php artisan db:seed
エラー:
Connection.phpの664行目:SQLSTATE [23000]:整合性制約違反:1452子行を追加または更新できません:外部キー制約が失敗します(
food
。cards
、CONSTRAINTca rds_pid_foreign
FOREIGN KEY(pid
)REFERENCESproducts
(id
))(SQL:in sert intocards
(quantity
、status
、cid
、pid
、updated_at
、creat ed_at
)の値(履歴書asperiores eligendi ipsam exercitationem quidem。、1、
18、8、2019年1月2日午前4時22分10秒、2019年1月2日午前4時22分10秒))Connection.phpの458行目:SQLSTATE [23000]:整合性制約違反:1452子行を追加または更新できません:外部キー制約が失敗します(
food
。cards
、CONSTRAINTca rds_pid_foreign
FOREIGN KEY(pid
)REFERENCESproducts
(id
))
乱数の代わりに、製品と顧客のIDを使用する必要があります。
$factory->define(App\Card::class, function (Faker $faker) {
$p_ids = App\Product::pluck('id')->toArray();
$c_ids = App\Customer::pluck('id')->toArray();
return [
//'id'=>$faker->numberBetween($min = 1, $max = 20),
'quantity'=>$faker->sentence($nbWords = 6, $variableNbWords = true),
'status'=>$faker->boolean($chanceOfGettingTrue = 10),
'cid'=>$faker->randomElement($c_ids),
'pid'=> $faker->randomElement($p_ids),
];
});
要するに、からすべてのIDを選択products
してcustomers
、テーブルを、外部キーとして使用するためにそれらからランダムな値をとります。
ただし、工場が特定の順序になっていることを確認してください。そのため、とのCardFactory
後に解雇する必要がProductFactory
ありCustomerFactory
ます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加