简体   繁体   English

我需要在laravel中返回更改。 数组{}到[]的对象(尝试与'[object Object]进行比较时出错)

[英]I need change return in laravel. object to array {} to [] (Error trying to diff '[object Object])

I need change return object to array in laravel api. 我需要将返回对象更改为Laravel API中的数组。
anyone guide? 有人指导吗? I am getting error, cant figure out why?? 我遇到错误,无法找出原因??

change {} to this [] {}更改为此[]

ERROR Error: "Error trying to diff '[object Object]'. Only arrays and iterables are allowed (API Laravel and front-end angular 7) 错误错误:“尝试区分'[对象对象]'时出错。仅允许数组和可迭代对象(API Laravel和前端angular 7)

my code api Laravel 我的代码 api Laravel

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();
        return (array)$this->processarCategoria($arCategoria);
    }
    public function processarCategoria($arCategoria){
        $ar = array();
        $cont = 0;
        foreach($arCategoria as $key => $value){
            $ar[$value['no_categoria'].'_'.$value['cd_categoria']][] =  (array)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 (array)$ar;
    }

my return of laravel api 我的Laravel API返回

{
    "Documentation_3": [
        {
            "no_link": "stackoverflow",
            "cd_link": 5,
            "vl_link": null,
            "bo_ativo": 1,
            "link": "https://stackoverflow.com"
        },
        {
            "no_link": "Adventures of Time",
            "link": "http://adventuresoftime.com.br"
        }
    ],
    "Things to buy_5": [
        {
            "no_link": "Games",
            "link": "Games.com.br"
        }
    ]
}

If dd($this->processarCategoria($arCategoria->toArray())) is a plain PHP array, which means it works. 如果dd($this->processarCategoria($arCategoria->toArray()))是纯PHP数组,则表示它可以工作。

Btw, I think your original code works,too, maybe you dd((array) $this->processarCategoria($arCategoria)) and check. 顺便说一句,我认为您的原始代码也dd((array) $this->processarCategoria($arCategoria)) ,也许您dd((array) $this->processarCategoria($arCategoria))并检查。


According to PHP.net , (array)$scalarValue is exactly the same as array($scalarValue) 根据PHP.net ,(array)$ scalarValue与array($ scalarValue)完全相同

According to Laravel document , The toArray() method converts the collection into a plain PHP array. 根据Laravel文档 ,toArray()方法将集合转换为纯PHP数组。


And the reason why the array become a JSON after return , as if someone using the json_encode PHP function, is because: 而数组在return之后变成JSON的原因(就像有人使用json_encode PHP函数)是因为:

Laravel automatically convert the array into a JSON response, in addition to returning strings from your routes and controllers. 除了从路由和控制器返回字符串外,Laravel还将数组自动转换为JSON响应。 Laravel document Laravel文件

Ie, laravel convert array to JSON, so it may show on the screen. 即,laravel将数组转换为JSON,因此它可能会显示在屏幕上。

Route::get('/', function () {
    //return is the same as dd()
    //dd(json_encode([1=>'a', 2=>'b', 3=>'c']));
    return [1=>'a', 2=>'b', 3=>'c'];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM