简体   繁体   English

Laravel数据库播种器,设置特定值laravel 5

[英]Laravel database seeder, setting specific values laravel 5

I am trying to populate my databases and have a parent status table and a child server_statuses table. 我正在尝试填充数据库,并有一个父status表和一个子server_statuses表。 This is a one-to-many relationship. 这是一对多的关系。

The issue i'm running into is that the statuses table only has 3 values and they need to be set to specific values ( success, warning, danger ). 我遇到的问题是,状态表只有3个值,需要将它们设置为特定值( success, warning, danger )。 Larvaels documentation doesn't go into much depth on how to accomplish this. Larvaels文档对如何完成此工作没有深入探讨。

Im also not sure if the seeding is the reason im getting the error, posted below. 我还不确定播种是否是收到错误的原因,发布如下。 If it isnt the seeding I was thinking I could possibly hand enter the 3 values within PHPmyAdmin then run my DB seeder. 如果不是种子,我想我可以在PHPmyAdmin中手动输入3个值,然后运行数据库种子。 I will also post my migration as well as the seeder files. 我还将发布迁移文件和种子文件。

Any thoughts on a solution to this error? 对解决此错误有任何想法吗?

Current error when running the seeder: 运行播种机时的当前错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constrai
ENCES `statuses` (`id`)) (SQL: insert into `server_statuses` (`server_id`, `status_id`, `updated_at`, `created_at`) values (20, 4, 2018-07-03 12:21:05,

Migrations: 迁移:

 Schema::create('statuses', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('key');
            $table->string('status');
            $table->timestamps();
        });

 Schema::create('server_statuses', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->integer('server_id')->unsigned();
            $table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade');
            $table->integer('status_id')->unsigned();
            $table->foreign('status_id')->references('id')->on('statuses');
            $table->timestamps();
        });

Seeder factory(deletd what I was trying for the Status because it was a terrible attempt at a solution): 播种机工厂(删除了我正在尝试的“状态”,因为这是对解决方案的一次可怕尝试):

$factory->define(Status::class, function (Faker $faker) {

    return [
        'key' => ,
        'status' => 
    ];
});

$factory->define(ServerStatus::class, function (Faker $faker) {
    return [
        'server_id' => $faker->numberBetween($min = 1, $max = 20),
        'status_id' => $faker->numberBetween($min = 1, $max = 3)
    ];
});

This error is because you are inserting entries into server_statuses with status_id which not present in status table. 此错误是因为您要在status表中不存在的条目中插入带有status_id server_statuses First make entries in status with id 1 to 3 and then run command to insert data in server_statuses . 首先输入ID为1到3的status条目,然后运行命令在server_statuses插入数据。

Based on your last comment. 根据您的最新评论。 The way to seed database from array. 从数组播种数据库的方法。

Create a seeder class: 创建一个播种器类:

use Illuminate\Database\Seeder;
use App\Models\Status;

class StatusSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run() {
        DB::table('status')->delete();
        $statuses = [
            ['key' => 1, 'status' => '1'],
            ['key' => 2, 'status' => '2'],
        ];
        foreach ($statuses as $status) {
            Status::create(array(
                'key' => $status["key"],
                'status' => $status["status"],
            ));
        }
    }

}

And add its entry in default DatabaseSeeder.php 并将其条目添加到默认的DatabaseSeeder.php

public function run()
    {
         $this->call(StatusSeeder::class);
    }

Run the seeding process by php artisan db:seed . 通过php artisan db:seed运行播种过程。 For more information, visit here . 有关更多信息,请访问此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM