Giovannefc,
Your collection looks like a multi-dimensional array. If that's the case wouldn't you need to do a foreach inside of the results foreach you already have in place?
-- Daniel McCarthy
This collection (from cart package) works:
object(Darryldecode\Cart\CartCollection)#236 (1) {
["items":protected]=>
array(1) {
[6]=>
object(Darryldecode\Cart\ItemCollection)#188 (1) {
["items":protected]=>
array(6) {
["id"]=>
string(1) "6"
["name"]=>
string(37) "Losartana 50mg c/30 comp Neo Química"
["price"]=>
float(6.95)
["quantity"]=>
string(1) "1"
["attributes"]=>
object(Darryldecode\Cart\ItemAttributeCollection)#189 (1) {
["items":protected]=>
array(0) {
}
}
["conditions"]=>
array(0) {
}
}
}
}
}
foreach ($results as $result) {
echo $result->id;
}
Result:
6
My collection works this way:
foreach ($results as $result) {
echo $result['id'];
}
But what is the difference of the two collections?
I would like to works as object the same eloquent collections, for example.
Thanks.
Not all collection are same. Your collection is of type Illuminate\Support\Collection, while the other is of type Darryldecode\Cart\CartCollection. The Darryldecode\Cart\CartCollection class probably has a magic getter wich interprets $collection->something as $collection['something']. Don't try to change it. Instead do something like this.
foreach ($tipos as $tipo => $value) {
$consulta = $this->calcFrete($tipo, $this->cep, $atributos);
$class = new stdClass();
$class->id = "$tipo";
$class->nome = $value;
$class->valor = str_replace(',', '.', $consulta->Valor);
$class->prazo = $consulta->PrazoEntrega;
$retorno[$tipo] = $class;
}
return collect($retorno);
Reason for error is because your Collection only contains one array, "items" and it is an array of Collections, it does not contain a key matching "id" Try cycling through the array within your collection, like this:
foreach ($results->items as $result) {
echo $result->id;
}
Note: I would also have code to check $results is not empty.
Firtzberg said:
Not all collection are same. Your collection is of type Illuminate\Support\Collection, while the other is of type Darryldecode\Cart\CartCollection. The Darryldecode\Cart\CartCollection class probably has a magic getter wich interprets $collection->something as $collection['something']. Don't try to change it. Instead do something like this.
foreach ($tipos as $tipo => $value) { $consulta = $this->calcFrete($tipo, $this->cep, $atributos); $class = new stdClass(); $class->id = "$tipo"; $class->nome = $value; $class->valor = str_replace(',', '.', $consulta->Valor); $class->prazo = $consulta->PrazoEntrega; $retorno[$tipo] = $class; } return collect($retorno);
Hello Firtz. Returns this error:
Cannot use object of type stdClass as array
On this line:
$retorno[$tipo] = $class;
Thank you.
christoferd said:
Reason for error is because your Collection only contains one array, "items" and it is an array of Collections, it does not contain a key matching "id" Try cycling through the array within your collection, like this:
foreach ($results->items as $result) { echo $result->id; }
Note: I would also have code to check $results is not empty.
Hello christoferd
Returns this error:
Cannot access protected property Illuminate\Support\Collection::$items
Thank you.
Is $retorno an array? It should stay an array, only the handled objects should be classes.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community