Back

Modify a json file with laravel


Jrecos posted 1 year ago

as I can modify a json in laravel file? In the methods available for the File class, only allows me to add additional text:

File::append($path, $data);

however, can not find a method to modify.

example:

public function json(){
      $data = json_encode([
         'data1' => 'hello',
          'data2' => 'world' ]);
  File::append($path, $data);
}

public function ModifyJson(){

//how change value for data2?  

}

thank you very much opr the help :)

astroanu replied 1 year ago

you will need to read the file, json decode it to a php object set the required value, then write it again back json decoded.

File::put('path', json_encode($object));
savanihd replied 2 weeks ago

You can also see this example: https://hdtuto.com/article/how-to-read-and-write-json-file-in-php-laravel

I hope it can help you

Pardeeptech replied 1 week ago

Use like ,

$data = json_decode($your_file_content,true),

then modify $data as normal php array, and before save use json_encode($data) and save data.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us