klp klp - 5 months ago 10
PHP Question

I am trying to insert json data into a file

I am inserting json data to a file in laravel,i have 3lakh records and i am getting it as 5000 one time again incrementing in post man.

public function fileput($start,$end,$n) {
$final = [];
$res = DB::table('company')
->Where('company_id', '>',$start)
->Where('company_id', '<',$end)
->get();

foreach ($res as $k => $v) {
$id = $v->company_id;
$index = 10000000 + $id;
unset($res[$k]->company_id);
unset($res[$k]->company_data);
$arr = array('index' => ['_id' => $index]
);
$ind = json_encode($arr);
$data = json_encode($v);
array_push($final, $ind, $data);
}
$written = File::put('file.txt', $final);
}


The problem is,after entering first 5000 when i increment it to 10000,the first 5000 got erased.Therefore how can i insert all the records in one file.

Answer

Try using File::append() instead of File::put().