【问题标题】:ErrorException array_column() expects parameter 1 to be array, object givenErrorException array_column() 期望参数 1 是数组,给定对象
【发布时间】:2021-05-07 12:51:27
【问题描述】:
   public function  goesWellWith(Request $request)
        {
            $seo_url = $request->seo_url;
            $product = $this->product->getByAny('seo_url',$request->seo_url)->first();
            $categories = $this->product->getProductCategories($product->id);
            $category_ids = [];
            $products = [];
    
            if(count($categories) > 0){
                $category_ids = array_column($categories, 'term_id');
            }
    
            if(count($category_ids) > 0){
    
                $get_fillter_product = $this->product->getProductByFilter([
                    'recommended' => "on"
                ],$category_ids);
    
                foreach($get_fillter_product as $single_product){
    
                        $categoryProductList = [
                            'title' => $single_product->title,
                            'sub_title' => $single_product->sub_title,
                            'first_image' => $single_product->first_image?$single_product->first_image->full_size_directory: null,
                            'second_image' => $single_product->second_image?$single_product->second_image->full_size_directory: null,
                            'seo_url' => $single_product->seo_url
                        ];
    
                        $category_ids [] = $categoryProductList;
                }
            }
   
            return response()->json(compact('category_ids'));
    
        }

//

-

列表项

当我在失眠中点击 url 来检查时,我得到了这个 ErrorException array_column() 期望参数 1 是数组,给定对象//

我在这行代码中遇到了错误

if(count($categories) > 0){
                $category_ids = array_column($categories, 'term_id');
            }

【问题讨论】:

    标签: laravel


    【解决方案1】:

    尝试从这里更改您的代码

    $categories = $this->product->getProductCategories($product->id);
    

    $categories = $this->product->getProductCategories($product->id)->toArray();
    

    【讨论】:

    • 我从这段代码中得到了错误 ,,,, if(count($categories) > 0){ $category_ids = array_column($categories, 'term_id'); }
    • 你试过我的建议了吗?这是因为您的 $categories 是一个对象。您必须先将其更改为数组。正如你的错误所说expects parameter 1 to be array, object given
    • 是的,先生,我尝试了您建议的那个,它不起作用。但我现在再次检查它的工作。我犯了一个错误 。谢谢先生
    • 如果您使用了我建议的代码并且有效,请接受我的回答。谢谢你:)
    • 谢谢先生。终于成功了!在您的建议的帮助下。
    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 2018-11-28
    • 2018-11-27
    • 2021-02-06
    • 1970-01-01
    • 2019-09-05
    • 2015-04-30
    • 2014-02-08
    相关资源
    最近更新 更多