1

This is my code:

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();

I have added two new fields type and icon and when i run seeders it says:

In privilegesTableSeeder
Undefined index: icon

I dont know where is issue:

This is my array:

['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'],

Can you guys sort out this problem? Thanks in advance

bradrn
  • 8,337
  • 2
  • 22
  • 51
syed1234
  • 761
  • 5
  • 12
  • 30

3 Answers3

0

The first array element does not have an icon key to be accessed. If the icon database table row can be nullable and you don't want to set any icon define the key but set a null value like: 'icon' => null

namelivia
  • 2,657
  • 1
  • 21
  • 24
0

It happens because there is no element call icon in the first array, so if you have to set the default value for non-required elements just like below

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();
Sethu
  • 1,299
  • 10
  • 13
0

Create model :

php artisan make:model PrevDefinition

In model:

<?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'];
}

In seeder:

<?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);
        }
    }
}