jack98
jack98

Reputation: 69

insert data into 2 database using foreach loop

I need to insert 2 data in 2 database. value "company" can be null and can have data."company"is inserted into different table. the problem is, if insert data with "company" is null, the error will be ErrorException Undefined array key. how to insert different data in different table? each table related with each other with foreign key -> id.

if (is_countable($data) && count($data) > 0){
      
            foreach ($data['mm_name'] as $item => $value) {
                $data2 = array(
                    'um_id' => $meeting->id,
                    'mm_name' => $data['mm_name'][$item],
                    'mm_email' => $data['mm_email'][$item],
                );
                MeetingMember::create($data2);
            }
        }

        if (is_countable($data) && count($data) > 0){
                foreach ($data['company'] as $company => $value) {
                    $data3 = array(
                        'meeting_id' => $meeting->id,
                        'company' => $data['company'][$company],
                        
                    );
                    AuditMeeting::create($data3);
                }
            }

Upvotes: 1

Views: 65

Answers (1)

Navid Bakhtiary
Navid Bakhtiary

Reputation: 1

instead of $data['mm_name'][$item] must use $value and instead of $data['company'][$company] must use $value.

and

change

if (is_countable($data) && count($data) > 0){
.
.
.

to

if (is_countable($data) && count($data) > 0 && isset($data['company'])){
.
.
.
.

Upvotes: 0

Related Questions