Empty Json Index

I have developed an Ionic application that communicates with my Laravel-based API by sending JSON data.

Whenever a post request is made to the designated route, I invoke a method on my controller.

This controller then takes the received data and stores it in the database. However, there are instances when not all of the expected data from the Ionic application is included.

For example, my Laravel application expects fields such as First Name, Last Name, and Company. Yet, sometimes the Ionic application only sends first_name and last_name, omitting the index for company.

How can I check in my controller if a certain index exists?

Take a look at the JSON request:

{"data":{"first_name":"Peter","last_name":"tESt"}}

Now, observe my controller waiting for the data request:

public function store(Request $request)
{

    $card = NEW card;
    $card->first_name = $request->data['first_name']; 
    $card->last_name = $request->data['last_name']; 
    $card->location = $request->data['location']; 
    $card->save();

};

In some scenarios, the location may be included, while in others, it may be excluded. The application ensures that empty indexes are not sent.

Answer №1

When programming in PHP, you have the ability to verify if a specific key exists within an array by utilizing the function array_key_exists.

To demonstrate this concept, consider the following code snippet:

if (array_key_exists('place', $request->data)) {
    $item->place = $request->data['place'];
}

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

A node is receiving a JSON object through an axios POST request

I am working on a project where I have two unique URLs. The first URL receives a form action from an HTML page along with data and then makes a post request to the second URL. The second URL, in turn, receives a JSON and uses Express to make a GET request ...

methods for transferring javascript variables to modal

<div> <h5> <a href="<?php echo base_url(); ?>/vendor/home/projects" >Return to Projects</a> </h5> <div> <div class="container"> <div class="row"> ...

Error in PHP script when making an AJAX call

First of all, I want to thank you all for your help. The script is creating a table but sending empty information. I have tried to modify it like this: However, when I call the script, it gives me an error: So, I have edited the script code to clean it ...

Is it possible to display images in PHP when the value matches the specified string?

Is there a faster and more efficient way to display one or multiple images if $value == $string? For instance, let's say I have a cell containing the string 'r o g'. If a user inputs ro, it should output <img src="red.gif"> and <im ...

Using Cordova to create an accordion list on an HTML webpage

I am in need of an accordion list implementation for the given HTML code. I specifically want it to expand when 'Technical' is clicked, and collapse upon clicking again. Here is the HTML code: <!-- Techincal --> <div class= ...

Script for calculating in PHP

I've scoured the internet in search of a sample script that meets my specific needs, but unfortunately, I have come up empty-handed. Essentially, I am looking to retrieve a value from a div and then multiply that number based on user input. For instan ...

Spring JSON Neo4j is a powerful combination for building robust

Imagine a scenario where there is a Person node with 3 hobbies: Peter -> Hockey Peter -> Soccer Peter -> Basketball When querying the data from a string using Neo4jRepository: interface PersonRepository extends Neo4jRepository<Person, Long>{ ...

What is the most effective method for serializing SVG data from the client's Document Object Model (DOM

As I delve into the world of creating interactive SVG/AJAX interfaces, I find myself faced with a challenge. Users are constantly manipulating elements on-the-fly and I want to give them the option to export their current view as a PNG image or an SVG docu ...

Divide the string using brackets as delimiters

I'm having trouble splitting a string by brackets and getting extra empty values in my array. I attempted to use the solution from a similar question that was previously answered, but it splits the string while also introducing empty values. // faq ...

"PHP error: Trying to access an undefined variable in an included

As a PHP beginner using XAMPP to create a website, I encountered an issue with the code below. In my db.php file: <?php $hostName='localhost'; $userName='xxxxxxxxx'; $userPass='xxxxxxxxx'; ?> In my test.ph ...

Verify the presence of a boolean false value in mustache.js

As stated in this particular query, the method of checking for false within a json object passed to a mustache template is as follows: {{^like}} it is false {{/like}} {{#like}} it is true {{/like}} Assuming our json data appears like this {"like" ...

The ngIf directive in Ionic 2 does not refresh after a user logs in

I'm facing an issue with the *ngIf directive in Ionic 2. Below is my code: <div *ngIf="isLogin" class="profile-info ng-binding padding text-center" (click)="openPage(accountPage)"> {{userEmail}} <span menu-toggle="menu-togg ...

Is it possible to utilize enums as keys in a Json structure?

I am currently utilizing TypeScript in conjunction with Node.js (MEAN stack). My aim is to incorporate an enum within the property/schema of a JSON object. An example of the enum would be: enum KeyEnums { A: "featureA", B: "featureB&qu ...

angularjs: Include an additional column into the existing json dataset

Is there a way to dynamically add a color column to the "solutions" table using angularjs and json? I tried this code: $scope.solutions.push({"color": value2.color}); I'm using a forEach loop to dynamically decide the value of value2.color, but I wa ...

Combining the power of jQuery, PHP, JavaScript, and the popular WordPress platform, let's unlock

After going through numerous attempts to find answers for similar issues, I'm unable to get any of the suggested solutions to work. My WordPress site requires a plugin that utilizes jQuery. The main file for my plugin is located at wp-content/plugins ...

A straightforward method to flatten and unflatten JSON files

I've managed to flatten JSON files effortlessly by executing this command in a combination of Node.js and Bash: npx flat foo.json > bar-flat-output.json. But now, I'm searching for another foolproof command that can easily undo the flattening ...

Presence of PHP File

Is there a way to check if a file exists using PHP? I attempted to use the file_exists() function, but encountered limited permissions and it seems that PHP is unable to read the file permissions. The file in question is an image that can be accessed via ...

Disallow specific sequences of characters

My contact form has been bombarded with spam messages lately. Upon investigation, I have observed a pattern in the content of these messages. The email address field always seems to include the phrase "Staceyrow," while the message field typically contains ...

Transforming lambda expressions into JSON objects using MongoDB .NET Driver

Struggling with mocking frameworks for unit tests, I find it easier to execute queries as strings rather than lambda expressions within my projects. This has led me to consider modifying my IDatabase interface: Interface IDatabase { IEnumerable<Use ...

Enhance your Magento experience by displaying various action links in a single column on the backend grid

Currently, I am in the process of creating a custom product list within the Magento backend. Here is the code snippet I am using to add a new row: $this->addColumn('action_widget', array( 'header' => Mage::helper( ...