Laravel 5 Seeder-数据库中的多行 [英] Laravel 5 Seeder - multiple rows in DB

查看:55
本文介绍了Laravel 5 Seeder-数据库中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以插入这样的多行(或类似的东西):

I was wondering if it's possible to insert multiple rows like this (or something like this):

<?php

use Illuminate\Database\Seeder;

class SettingTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('settings')->insert(
            [
                'key' => 'username',
                'value' => 'testusername'
            ],
            [
                'key' => 'password',
                'value' => 'plain'
            ]
        );
    }
}

我的数据库中有一个表设置,其中有 key&值.

I have a table settings in my database with columns key & value.

上面的代码的问题是他只插入了第一个....

The problem with the code above is that he only inserts the first one ... .

推荐答案

您需要将数组包装在另一个数组中,所以看起来像这样:

You need to wrap your arrays in another array, so it would look like this:

DB::table('settings')->insert([
    [
        'key' => 'username',
        'value' => 'testusername'
    ],
    [
        'key' => 'password',
        'value' => 'plain'
    ]
]);

注意包装数组.

您现在正在做的实际上是向insert()方法发送两个单独的数组.

What you are doing now is actually sending two separate arrays to the insert() method.

这篇关于Laravel 5 Seeder-数据库中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆