0

Why is this happening? I keep getting an error on this line of code: $groupcondition = $filter['conditions'][$index]; with the error message Undefined index: conditions. It clearly exists. Using Laravel to create columns. The conditions array is inside of the parent filter array, there is a parent filter array and a child filter array.

dump($filter):

  "filter" => array:3 [▶
    0 => array:1 [▶
      0 => array:3 [▶
        "code" => array:1 [▶
          "code" => "Thank You Codes"
        ]
        "condition" => array:1 [▶
          "condition" => "AND"
        ]
        "value" => array:2 [▶
          "title" => "BD"
          "desc" => "Board Member"
        ]
      ]
    ]
    1 => array:1 [▶
      0 => array:3 [▶
        "code" => array:1 [▶
          "code" => "Thank You Codes"
        ]
        "condition" => array:1 [▶
          "condition" => "AND"
        ]
        "value" => array:2 [▶
          "title" => "AU"
          "desc" => "Auction Donation Thank You"
        ]
      ]
    ]
    2 => array:1 [▶
      0 => array:3 [▶
        "code" => array:1 [▶
          "code" => "Thank You Codes"
        ]
        "condition" => array:1 [▶
          "condition" => "AND"
        ]
        "value" => array:2 [▶
          "title" => "AM123"
          "desc" => "am 123"
        ]
      ]
    ]
  ]
  "conditions" => array:3 [▶
    0 => array:1 [▶
      "condition" => "AND"
    ]
    1 => array:1 [▶
      "condition" => "OR"
    ]
    2 => array:1 [▶
      "condition" => "AND"
    ]
  ]
]

Function:

public function createCampaign($filter, $new_campaign)
    {
        foreach ($filter['filter'] as $index => $group) {
            $groupcondition = $filter['conditions'][$index];
            $filtergroup = FilterGroups::create([
                'campaign_id' => $new_campaign->id,
                'condition' => $groupcondition['condition']
            ]);
            foreach ($group as $filter) {
                $field = $filter['code']['code'];
                // change field name to donor perfect column name
                if ($field == 'Thank You Codes') {
                    $field = 'ty_letter_no';
                } else if ($field == 'General Ledger Codes') {
                    $field = 'gl_code';
                } else if ($field == 'Campaign Codes') {
                    $field = 'campaign';
                } else if ($field == 'Solicit Codes') {
                    $field = 'solicit_code';
                } else {
                    $field = 'sub_solicit_code';
                }

                Filters::create([
                    'filter_id' => $filtergroup->id,
                    'field' => $field,
                    'value' => $filter['value']['title'],
                    'condition' => $filter['condition']['condition']
                ]);
            };
        }
    }
  • Are you confident the array you pass to your `createCampaign` function always contains a `conditions` key? Testing your code in [phpsandbox](https://phpsandbox.io/n/quiet-silence-o1mo-jhayc) doesn't throw an error at the line you state for the example you have provided. – Peppermintology Apr 01 '21 at 14:47
  • There will always be a matching condition for every filter. This is the exact filter array I am receiving the error message on. – Courtney Erickson Apr 01 '21 at 14:48
  • You have not provided enough information to be sure, but it would appear that the "filter" and "conditions" keys are both on the same level. You are expecting there to be a "conditions" key inside each "filter" array, and there is not. – miken32 Apr 01 '21 at 15:08
  • You're not reading the question correctly. There is a filter array inside of an outer filter array that contains both the filter and conditions array. – Courtney Erickson Apr 01 '21 at 15:10
  • i think you should change `dump($filter):` to `if (!isset($filter['conditions'])) { dump($filter); }` – Alex Harris Apr 01 '21 at 16:37
  • I'm just showing the dump so you know what the output is. – Courtney Erickson Apr 02 '21 at 13:56

0 Answers0