0
I’m riding the return of father and daughter categories through a foreach
, but the return is coming in the front-end
as a object
and not a array
and what should I do in Laravel
to the front-end
make it through.
ERROR Error: "Error trying to diff '[object Object]'. Only arrays and iterables are allowed
Below is an example.
public function show($id)
{
$arCategoria = \App\Favorito::join('categoria', 'categoria.cd_categoria', '=', 'link.cd_categoria')
->select('*')
->where('categoria.cd_categoria_pai',$id)
->where('link.cd_usuario',$this->token['cd_usuario'])
->where('link.bo_ativo',true)
->get();
//montando o array para retorno
return $this->processarCategoria($arCategoria->toArray());
}
public function processarCategoria($arCategoria){
$array = array();
$cont = 0;
foreach($arCategoria as $key => $value){
$array[$value['no_categoria'].'_'.$value['cd_categoria']][] = array(
'no_link'=>$value['no_link'],
'cd_link'=>$value['cd_link'],
'vl_link'=>$value['vl_link'],
'bo_ativo'=>$value['bo_ativo'],
'link'=>$value['link']
);
$cont++;
}
return json_decode(json_encode($array), true);;
}
Return
{
"Documentações_3": [
{
"no_link": "stackoverflow",
"cd_link": 5,
"vl_link": null,
"bo_ativo": 1,
"link": "https://stackoverflow.com"
},
{
"no_link": "Adventures of Time",
"cd_link": 9,
"vl_link": null,
"bo_ativo": 1,
"link": "http://adventuresoftime.com.br"
}
],
"Datas comemorativas_5": [
{
"no_link": "Games",
"cd_link": 10,
"vl_link": null,
"bo_ativo": 1,
"link": "Games.com.br"
}
]
}
Front-end
service ts.
getLinksByIdusuario(id:number):Observable<any[]> {
return this.http.get<any[]>(`${API}/favorito/${id}`)
.pipe(map((data: any) => data ),
catchError(error => { return throwError(error)})
);
}
Component.
ngOnInit() {
this.id = params['id'];
this.homeService.getLinksByIdusuario(this.id)
.subscribe(
categorias => {
this.categorias = categorias
}
)
}
Component.html
<div class="row">
<div *ngFor="let categoria of categorias | keyvalue">
{{categoria.key}}
<div *ngFor="let cat of categoria">
{{cat.no_link}}
</div>
</div>
</div>
This mistake is not giving in to your
Front-End
???– novic
@Virgilionovic, this, fron-end
– Herick
The problem is not in PHP friend is how you treat this data in your
Front
has how to put the code of theFront
?– novic
@Virgilionovic updated my question by adding the front
– Herick
where is the error? if you know the line where to
– novic