I have a little doubt about a loop that I am doing, because it is returning empty values.
First, I am receiving my items like this:
$items = Session::get('items');
$deco = json_decode($items, true);
If I return $deco
I get this:
{
"items":[
{
"id":1,
"inventoryID":1,
"title":"Product 1",
"quantity":1,
"unit_price":20,
"image":"img.png"
},
{
"id":2,
"inventoryID":1,
"title":"Product2",
"quantity":1,
"unit_price":25,
"image":"img.png"
}
]
}
Now, in the loop (which I use to change the values of some keys for the integration of paypal) I have this:
$results = [];
foreach($deco['items'] as $element) {
$name=$element['title'];
$quantity=$element['quantity'];
$sku= $element['id'];
$price=$element['unit_price'];
$item = new Item();
$item -> setName($name)
->setCurrency('USD')
->setQuantity($quantity)
->setSku($sku)
->setPrice($price);
$results[]=$item;
}
If I return return $item
(outside the loop) I get (as expected) only one value, and not all of the collection:
{
"name": "Product2",
"currency": "USD",
"quantity": 1,
"sku": 2,
"price": "25"
}
But if I return return $results
the var that I need, it gives me this (it does the {} of the items as I need, but returns empty):
[
{},
{}
]
Thw whole code looks like:
public function test(){
$items = Session::get('items');
$deco = json_decode($items, true);
$results = [];
foreach($deco['items'] as $element) {
$name=$element['title'];
$quantity=$element['quantity'];
$sku= $element['id'];
$price=$element['unit_price'];
$item = new Item();
$item -> setName($name)
->setCurrency('USD')
->setQuantity($quantity)
->setSku($sku)
->setPrice($price);
$results[]=$item;
}
return $results;
}
EDIT: The Item.php
model at vendorpaypalrest-api-sdk-phplibPayPalApi
is the default PayPal’s:
class Item extends PayPalModel
{
/**
* Stock keeping unit corresponding (SKU) to item.
*
* @param string $sku
*
* @return $this
*/
public function setSku($sku)
{
$this->sku = $sku;
return $this;
}
/**
* Stock keeping unit corresponding (SKU) to item.
*
* @return string
*/
public function getSku()
{
return $this->sku;
}
/**
* Item name. 127 characters max.
*
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Item name. 127 characters max.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Description of the item. Only supported when the `payment_method` is set to `paypal`.
*
* @param string $description
*
* @return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Description of the item. Only supported when the `payment_method` is set to `paypal`.
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Number of a particular item. 10 characters max.
*
* @param string $quantity
*
* @return $this
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Number of a particular item. 10 characters max.
*
* @return string
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Item cost. 10 characters max.
*
* @param string|double $price
*
* @return $this
*/
public function setPrice($price)
{
NumericValidator::validate($price, "Price");
$price = FormatConverter::formatToPrice($price, $this->getCurrency());
$this->price = $price;
return $this;
}
/**
* Item cost. 10 characters max.
*
* @return string
*/
public function getPrice()
{
return $this->price;
}
/**
* 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/).
*
* @param string $currency
*
* @return $this
*/
public function setCurrency($currency)
{
$this->currency = $currency;
return $this;
}
/**
* 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/).
*
* @return string
*/
public function getCurrency()
{
return $this->currency;
}
//More similar stuff
Thank you in advance
2
Answers
The answer (thanks to Ionica on discord's laravel community) is:
It happens because of the JMS serialization only.
I guess in the controller level you might have to check is there any serialization groups has been applied or not.