Reputation: 183
I'm having a problem retrieving data from a select.
When I perform the following select
$Projeto = $this->ModelProjeto
->select('projeto.*','tipoprojeto.descricao')
->join('tipoprojeto','tipoprojeto.id','=','projeto.tipoprojeto')->get();
and returns json
[{"id":1,"descricao":"house","tipoprojeto":1,"tipoprojetodescricao":"Engenharia Civil"}]
When I try to retrieve the json id with the following command: return $Projeto->id;
error appears
Property [id] does not exist on this collection instance.
if I used the find method, it would work normally, but it is not possible to use join, so how do I access Project-> id using the -> get () method?
Upvotes: 0
Views: 30
Reputation:
The get()
method returns a collection, not an individual item.
$Projetos = $this->ModelProjeto
->select('projeto.*','tipoprojeto.descricao')
->join('tipoprojeto','tipoprojeto.id','=','projeto.tipoprojeto')
->get();
// you would need to loop the $Projetos to get each id
$Projetos->each->id
// or use the first() method if you know only one item is returned
$Projeto = $this->ModelProjeto
->select('projeto.*','tipoprojeto.descricao')
->join('tipoprojeto','tipoprojeto.id','=','projeto.tipoprojeto')
->first();
Upvotes: 2
Reputation: 1944
Looking at the JSON, this is an array, so instead of using get()
, use first()
$Projeto = $this->ModelProjeto
->select('projeto.*','tipoprojeto.descricao')
->join('tipoprojeto','tipoprojeto.id','=','projeto.tipoprojeto')->first();
This will make sure only 1 record is returned and that it will return an object instead of a collection
Upvotes: 2