1

I try to insert mutiple arrays per job_id when i try this my array values out put as null value this is my store function

$input output :

array:7 [▼ 
     "date" => "2016-01-10",
     "job_id" => "2",
     "charge" => array:2 [▼ 
           0 => "ch1",
           1 => "ch2" 
     ],
     "category" => array:2 [▼
          0 => "se1",
          1 => "se2" 
     ],
     "amount" => array:2 [▼
          0 => "100"
          1 => "200" 
     ]
 ]

Full Code :

$input = Input::all();

$charge = Input::get('charge');
$date = Input::get('date');
$job = Input::get('job_id');
$amount = Input::get('amount');
$cat = Input::get('category');
$id = \Auth::id();

$data = array();

foreach ($charge as $key=>$value ){
    $data[] = [
          'date' => $date,
          'charge'=>$key['charge'],
          'job_id'=>$job,
          'user_id'=>$id,
          'amount'=>$key['amount'],
          'category'=>$key['category'],
    ];
}

dd($data);

When i try to get out put i will get this out put

array:2 [▼
  0 => array:6 [▼
    "date" => "2016-01-10"
    "charge" => null
    "job_id" => "2"
    "user_id" => 1
    "amount" => null
    "category" => null
  ]
  1 => array:6 [▼
    "date" => "2016-01-10"
    "charge" => null
    "job_id" => "2"
    "user_id" => 1
    "amount" => null
    "category" => null
  ]
]

What i want is i want insert data to my database any one suggest me good idea ?

Answer for this question from Laracast Check this link

1 Answer 1

1

Your final output looks correct, try to use the following code to insert the $data to your DB :

Model::insert($data); // Eloquent
DB::table('table')->insert($data); // Query Builder

You should construct your array using $inputs['amount'][$key] for amount value and $inputs['category'][$key] for category, like following :

foreach ($charge as $key=>$value ){
    $data[] = [
          'date' => $date,
          'charge'=>$value,
          'job_id'=>$job,
          'user_id'=>$id,
          'amount'=>$inputs['amount'][$key],
          'category'=>$inputs['amount'][$key],
    ];
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

8 Comments

:) ya but im trying get out put so i used dd() to check my ouput so im getting null
Okay try to do dd($input) and add the output to the OP.
yes this is output array:7 [▼ "date" => "2016-01-10" "job_id" => "2" "charge" => array:2 [▼ 0 => "ch1" 1 => "ch2" ] "category" => array:2 [▼ 0 => "se1" 1 => "se2" ] "amount" => array:2 [▼ 0 => "100" 1 => "200" ] ]
when i try this foreach loop foreach ($charge as $key ){ $data[] = [ 'date' => $date, 'charge'=>$key, 'job_id'=>$job, 'user_id'=>$id, 'amount'=>$amount, 'category'=>$cat, ]; } charges i can insert but amount and category first value insert all the charges
please edit your answer 'charge'=>$key['charge'], as 'charge'=>$value its working perfect edit yours plz
|

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.