How to generate a custom JSON key name in PHP using Laravel

Currently, I am utilizing imagick to extract a number of pages and then store it in JSON format. However, the number is stored as is without any key, for instance - 20. I am seeking a way to store the number with a specific key like below:

{
   "pages": "20"
}

Below is an example of the code that I have. $json represents the path to the newly created JSON file, while $num signifies the total number of pages.

Storage::put(($json), $num);

My question is should I first create the JSON file, then find a way to add a key to the number before encoding the file?

Answer №1

    /**
     * Save the data to a file.
     *
     * @param string $filePath
     * @param string|resource $data
     * @param mixed $settings
     * @return bool 
     * @static 
     */ 
    public static function saveData($filePath, $data, $settings = array())
    {
        return \Illuminate\Filesystem\FilesystemAdapter::saveData($filePath, $data, $settings);
    }

The second argument for Storage::saveData should be the raw data you want to store in the specified path. Therefore, you must first encode your metadata into the desired format.

For example:

$info = ['pages' => $num];
Storage::saveData($jsonFilePath, json_encode($info));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Cannot access Codeigniter subfolder

I am facing an issue with codeigniter htaccess and need some assistance. My demo site URL is sampleurl.com and all my files are located in a subfolder named demo. -sampleurl.com -demo - application - system - index.php -demo1 I hav ...

Enjoy the seamless integration of SPA Vue frontend with backend Laravel 7 using Sanctum for

Having trouble with my Vue and Laravel Spa setup using Sanctum. When sending requests through axios, I keep getting a status of 204. When trying to retrieve user information, I'm getting an Unauthenticated error message. Not sure what's going wro ...

Search results in Eclipse are not displaying the filename

After extensive searching across multiple search engines, it seems like I am the only developer facing this particular issue. It has been affecting my productivity for years now. My problem arises when I perform a File Search in Eclipse 3.7.2 on Centos (a ...

Tips on organizing Stripe charges based on identifier

I have a vision to develop a Donation Website that organizes donation amounts by team. My goal is to utilize Stripe for processing all charges and then categorize them according to the teams involved. How can I calculate the total amount donated for each ...

Discovering the Ins and Outs of the strlen Function in

My database contains the following items: Item Description -------------------------- Item 1 Some text here Item 2 Some text Item 3 Some text here Item 4 Some Item 5 Some text here The client wants descriptions to be truncated ...

Keep the music playing by turning the page and using Amplitude.js to continue the song

I have implemented Amplitude.js to play dynamic songs using a server-side php script. My objective is to determine if a song is currently playing, and if so, before navigating away from the page, save the song's index and current position (in percenta ...

Node js does not support iteration for DirectoryFiles

I am currently working on a program aimed at mapping all the json files within a specific directory. As I am new to JavaScript, this inquiry may not be ideal or obvious. Here is my code: const fs = require('fs'); const { glob } = require('gl ...

Challenges with integrating Wampserver PHP 5.3.1 and Pear:DB

After successfully installing PHP5.3.1 on top of 5.3.0 on my Windows 7 Pro laptop, along with Smarty, Pear and relevant Pear packages, I have encountered a strange issue. I have separate development and production sites set up using a config.php file - the ...

Query to retrieve WooCommerce products based on category name using MySQL

I need a solution to update products based on their specific category. Currently, I have successfully retrieved the desired results using the following MySQL query: SELECT * FROM wp_posts WHERE post_type = 'product' At present, there are two pr ...

Unbelievable - 'Cross-Origin Resource Sharing' headache

When making an ajax request to http://mydomain.com.net/temp/getdata.php?File=something.txt from http://mydomain.com.net/myapp/web/(index.php), I am encountering the following issue: XMLHttpRequest cannot load http://mydomain.com.net/temp/getdata.php?F ...

Utilizing JSON and AJAX to mine data from databases

I've been working on creating a basic "search" box using Javascript, AJAX, PHP, and JSON. I'm focusing on the "world" database that I downloaded from the MySQL website for this particular project. I've hit a roadblock when trying to retrieve ...

Trouble accessing Laravel route using Ajax technology

In my main container (app.blade.php), I have a home.blade file which contains a container that is refreshed with ajax when an item in the sidenav menu is clicked. If you click on one of those items after your session has expired, a middleware should redire ...

Having trouble getting dropdown values to work properly in HTML?

Currently, I'm encountering an issue with a drop-down menu that contains the days of the month (ranging from 1 to 31). When I input "22," it seems to default to loading option "20" instead. Displayed below is the code snippet that I am currently usin ...

Displaying an array as a column yields no visible results

Yesterday I posted a query regarding displaying an array with commas as columns. You can view the original question here. Today, my progress is as follows: <?php require_once("dbconnection.php"); ?> <?php $sql = "SELECT amount, ingredients FROM ...

Exploring the array of nested objects within an array of objects

Greetings, I seem to be facing a challenge with my JSON data. My goal is to extract the "tag" value from the objects within the "cows" array. Delving into nested data structures is somewhat new to me, and I've hit a roadblock in this process. Any insi ...

Unleashing the Power of a Tailored JSON Format for Rails

Within my Rails application, I have implemented an action that generates a JSON string. The code snippet illustrating this functionality is as follows: If the user exists format.json { render json: {:msg => 'This user already exists'}} el ...

What steps can I take to gradually reduce the density of an array?

Having a fully-populated array of values, I am looking to remove elements from the array in an arbitrary manner, with more removals happening towards the far end. For instance, consider the following input (where a . signifies a populated index) ........ ...

Adding or removing a class using Jquery based on the condition of form validation

I am facing a problem with the following code that adds and removes classes to bring the next stage of the form. The form progresses step by step where certain fields need to be filled before validation on the next button, followed by filling other fields. ...

Implementing a loop in jQuery is not functioning as expected

Can someone help me figure out how to use a for loop to create a jQuery function? I'm having trouble when adding the array parameter in the string, it just doesn't seem to work correctly. Any suggestions? var ids=["width_uncheck","thickness_unch ...

The JSON-generated input form is not appearing in the $_POST method

Using json to fetch data from the database, I encounter an issue where certain input fields retrieved from JSON are not displayed when I print $_POST. print_r($_POST); // prints everything except the input fields obtained from JSON This is my PHP code: ...