0

I'm struggling to access all of the input array data in Laravel, as I was with no problem previously: e.g., $_POST['name'][$row]

I'm also receiving the following error, but I assume it's because of the NULL data.
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'

POST Data:

name    Array ( [0] => name1 [1] => name2 )
dob Array ( [0] => 06/23/16 [1] => 06/03/16 )
gender  Array ( [0] => 1 [1] => 0 )

Loop:

$jobID = 2;
$data = array();
foreach(Input::get('name') as $row=>$name){

    $dob = Input::get('dob'.$row);
    $gender = Input::get('gender'.$row);

    $data[] = "['job_id' => '$jobID', 'first_name' => '$name', 'dob' => '$dob', 'gender' => '$gender']";

}

$data_insert = implode(',', $data);

if(!empty($name)) {
    DB::table('job_data')->insert([
        $data_insert
    ]);

}

Resulting SQL:

insert into `job_data` (`0`) 
values (
  ['job_id' => '2', 'first_name' => 'name1', 'dob' => '', 'gender' => ''],
  ['job_id' => '2', 'first_name' => 'name2', 'dob' => '', 'gender' => '']
)

1 Answer 1

1

You dont have to stringify your data, just simply use this

$data[] = [
    'job_id' => $jobID, 
    'first_name' => $name, 
    'dob' => $dob, 
    'gender' => $gender
];

and insert it with

DB::table('job_data')->insert($data);
Sign up to request clarification or add additional context in comments.

3 Comments

Strike previous comment. Thank you, this takes care of the Unknown column error, but my data is still NULL except for name.
Change your line $dob = Input::get('dob'.$row); into $dob = Input::get('dob')[$row]; and also with the gender
or Input::get("dob.$row");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.