未定义的索引:运行laravel seeder时出现图标错误

时间:2019-05-20 09:17:26

标签: laravel migration laravel-seeding

这是我的代码:

DB::beginTransaction();
    for($i = 0; $i < count($privileges); $i++) {
        DB::table('prev_definition')->insert([
            'priv_title' => $privileges[$i]['priv_title'],
            'priv_key' => $privileges[$i]['priv_key'],
            'display_group' => $privileges[$i]['display_group'],
            'parent_id' => $privileges[$i]['parent_id'],
            'type' => $privileges[$i]['type'],
            'icon' => $privileges[$i]['icon'], // here is error
        ]);
    }
DB::commit();

我添加了两个新的字段类型和图标,当我运行播种机时说:

In privilegesTableSeeder
Undefined index: icon

我不知道问题出在哪里:

这是我的数组:

['priv_title' => 'Basic Contact','priv_key' => 'can_access_patient_basic_contact', 'display_group' => 'patient_basic_contact','parent_id' => 7,'type' => 'menu'],
['priv_title' => 'Create Task','priv_key' => 'patient_basic_contact_can_add_task', 'display_group' => 'patient_basic_contact','parent_id' => 13,'type' => 'create_task','icon' => 'create_task'],
['priv_title' => 'View','priv_key' => 'patient_basic_contact_can_view', 'display_group' => 'patient_basic_contact','parent_id' => 13,'type' => 'view','icon' => 'view'],
['priv_title' => 'Edit','priv_key' => 'patient_basic_contact_can_edit', 'display_group' => 'patient_basic_contact','parent_id' => 13,'type' => 'edit','icon' => 'edit'],
['priv_title' => 'Delete','priv_key' => 'patient_basic_contact_can_delete', 'display_group' => 'patient_basic_contact','parent_id' => 13,'type' => 'delete','icon' => 'delete'],

你们能解决这个问题吗? 预先感谢

3 个答案:

答案 0 :(得分:0)

第一个数组元素没有要访问的icon键。如果图标数据库表行可以为空,并且您不想设置任何图标,请定义键,但设置一个空值,例如: 'icon' => null

答案 1 :(得分:0)

发生这种情况是因为在第一个数组中没有元素调用icon,因此,如果您必须像下面那样为非必需元素设置默认值

DB::beginTransaction();
    for($i = 0; $i < count($privileges); $i++) {
        DB::table('prev_definition')->insert([
            'priv_title' => $privileges[$i]['priv_title'],
            'priv_key' => $privileges[$i]['priv_key'],
            'display_group' => $privileges[$i]['display_group'],
            'parent_id' => $privileges[$i]['parent_id'],
            'type' => $privileges[$i]['type'],
            'icon' => isset($privileges[$i]['icon']) ? $privileges[$i]['icon'] ? null; //set the default as NULL or something else
        ]);
    }
DB::commit();

答案 2 :(得分:0)

创建模型:

php artisan make:model PrevDefinition

在模型中:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PrevDefinition extends Model
{
    protected $table = 'prev_definition';

    public $timestamps = true;

    protected $fillable = ['priv_title', 'priv_key', 'display_group', 'parent_id', 'type', 'icon'];
}

在播种机中:

<?php

use Illuminate\Database\Seeder;
use App\PrevDefinition;

class privilegesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $privileges = [
            ['priv_title' => 'Basic Contact','priv_key' => 'can_access_patient_basic_contact', 'display_group' => 'patient_basic_contact','parent_id' => 7,'type' => 'menu'],
            ['priv_title' => 'Create Task','priv_key' => 'patient_basic_contact_can_add_task', 'display_group' => 'patient_basic_contact','parent_id' => 13,'type' => 'create_task','icon' => 'create_task'],
            ['priv_title' => 'View','priv_key' => 'patient_basic_contact_can_view', 'display_group' => 'patient_basic_contact','parent_id' => 13,'type' => 'view','icon' => 'view'],
            ['priv_title' => 'Edit','priv_key' => 'patient_basic_contact_can_edit', 'display_group' => 'patient_basic_contact','parent_id' => 13,'type' => 'edit','icon' => 'edit'],
            ['priv_title' => 'Delete','priv_key' => 'patient_basic_contact_can_delete', 'display_group' => 'patient_basic_contact','parent_id' => 13,'type' => 'delete','icon' => 'delete'],
        ];

        foreach ($privileges as $data) {
            PrevDefinition::create($data);
        }
    }
}