klp klp - 4 months ago 5x
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)

foreach ($res as $k => $v) {
$id = $v->company_id;
$index = 10000000 + $id;
$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.


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