I am doing this:
$product = new \Illuminate\Database\Eloquent\Collection($product_array);
But it works as an array not as an object like $product->subscription won't work, but $product['subscription'] would work.
I am doing this:
$product = new \Illuminate\Database\Eloquent\Collection($product_array);
But it works as an array not as an object like $product->subscription won't work, but $product['subscription'] would work.
You can use collections like this in an object-oriented way:
$product->get('subscription');
You can use ArrayObject with the ArrayObject::ARRAY_AS_PROPS flag to
$api = new \ArrayObject(collect([
'api_version' => 1,
'facebook_id' => 1,
]),2)
echo $api->facebook_id; // ouputs 1
Notice the 2 is just shorthand for ArrayObject::ARRAY_AS_PROPS to mask the uglyness.
There must be another way and we're just missing something. I'm on Laravel 5.4 and trying to use this in a method to take either $request or something like $api from the example.
$product = (object) $product_array;
$product->subscription;
Example :
$product_arr = [
'name' => 'bag',
'qty' => 4,
];
$product = (object) $product_arr;
$product->name; // result = bag