#Property [id] does not exist on this collection instance

25 messages · Page 1 of 1 (latest)

marsh hill
#

when I print the dd I have the id, but when I try to use it, it gives the error.

public function LoadcalculoSeguro(CalculoSeguroService $calculoSeguroService){
    if (!empty($this->atividadeCode))
    {
        $this->precos=$calculoSeguroService->getPrecos($this->atividadeCode);
       // dd ($this->precos);
       // here -  return error
           $this->calculos=$calculoSeguroService->getAll($this->precos->id, $this->atividadeCode);
    }
}
```php
thorny vessel
#

@marsh hill what error are you seeing. ?

marsh hill
thorny vessel
#

Yes that’s correct. It is because you have a collection. The property id is on the model, which is inside the collection.

marsh hill
#

humm...

thorny vessel
#

The getPrecos() method returns a collection, so you need to get the item you want from it.

marsh hill
#

the fact of showing the model in the DD does not mean that it is accessible

thorny vessel
#

DD is showing a collection of models.

#

Collection != Model

#

Your method should either return one item, or you need to do first() on the collection to get the first item.

marsh hill
#

I need to learn how to interpret DD better

#

collection is eloquent's default return

thorny vessel
thorny vessel
marsh hill
#

ok

#

ok, I'll read the documentation some more

thorny vessel
#

The solution is pretty simple. You just need to get the first item out of there. I’ve shown the method above.

#

But yes have a read. 👍

random wigeon
#

dd is showing you it's an array
the 0 is the index of the first element

marsh hill
#

a small detail that changes everything

#

public function getPrecos($atividadeCode)
{
return Preco::where('codigo', '=',$atividadeCode)->first();
}

#

It worked 🙂

#

thank you very much 🙂

#

had used the get
return Preco::where('codigo', '=',$atividadeCode)->get();