Hello. I have an application where I need to mount a grid with all the data in the produtos
table. This table has foreign keys for two other tables, grupo
and subgrupo
. To mount my grid, I need the array with the products to have the group and subgroup id and all the other information contained in an array.
My method in the controller:
public function grid(){
return \App\produto::all();
}
The product model has:
public function grupo(){
return $this->hasMany('App\grupo');
}
public function subgrupo(){
return $this->hasMany('App\subgrupo');
}
But I do not know how to build the grid using this. I need the return array to look something like this:
[
{
"codigo": 3,
"grupo": [
{
"codigo": 1,
"descricao": "Descrição do grupo"
}
],
"subgrupo": [
{
"codigo": 1,
"descricao": "Descrição do subgrupo"
}
],
"descricao": "1"
}
]
In addition, as you may notice I needed to change the default "id" to "code", but this is already done. But I do not know if this could affect the search.
I searched before but did not find what I needed, or maybe I did not search the right thing for lack of knowledge of Laravel's used names. Can someone help me, please?