Prudential Developer
Prudential Developer

Reputation: 157

Multiple array null value?

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

Upvotes: 1

Views: 74

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67525

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.

Upvotes: 1

Related Questions