Laravel筑巢json回应

时间:2017-07-23 19:08:48

标签: json laravel

我想在laravel的api控制器中创建像这个my json的json。 在这里控制器

$data = [];
        foreach ($products as $p => $product) {
            $components = [];
            $product_obj = new StdClass();
      $product_obj->id = $product->id;
            $product_obj->name = $product->name;
            $product_obj->category_id = $product->category_id;
            //$product_obj->category_name = $product->category->name;
            $product_obj->sku = $product->sku;
            $product_obj->weight = $product->weight;
            $product_obj->discount_type = $product->discount_type;
            $product_obj->additional_price = $product->additional_price;
            $product_obj->discount = $product->discount;
            $product_obj->start_discount = $date_start;
            $product_obj->end_discount = $date_end;
            $product_obj->thumbnail = $product->thumbnail;
            $product_obj->mime_content_type = $product->mime_content_type;
            $product_obj->description = $product->description;
            $product_obj->is_active = $product->is_active;

            //$product_obj->created_at = $product->created_at;
            //$product_obj->updated_at = $product->updated_at;

            foreach ($product->components()->get() as $c => $component) {
                $item_arr = [];
                $component_obj = new StdClass();
                $component_obj->id = $component->id;
                $component_obj->product_id = $component->product_id;
                $component_obj->item_id = $component->item_id;
                $component_obj->quantity = $component->quantity;
                $component_obj->is_mandatory = $component->is_mandatory;
                $component_obj->is_customqty = $component->is_customqty;
                //$component_obj->created_at = $component->created_at;
                //$component_obj->updated_at = $component->updated_at;
                $component_obj->quantity_step = $component->quantity_step;
                $component_obj->display_order = $component->display_order;
                $component_obj->quantity_display = $component->quantity_display;
                $component_obj->default_template = $component->default_template;

            foreach ($component->item()->get() as $i => $item) {
        $item_option_groups = [];

        //$options = [];
        $item_templates = [];
        $item_backgrounds = [];
        $item_background_images = [];

                    $item_obj = new StdClass();
                    $item_obj->id = $item->id;
                    $item_obj->name = $item->name;
                    $item_obj->price = $item->price;
                    $item_obj->width = $item->width;
                    $item_obj->height = $item->height;
                    $item_obj->allow_image_upload = $item->allow_image_upload;

                    foreach ($item->itemOptionGroups()->get() as $iog => $item_option_groups) {
                        $item_option_groups = [];

                        $item_option_groups_obj = new StdClass();
                        $item_option_groups_obj->id = $item_option_groups->id;
                        $item_option_groups_obj->name = $item_option_groups->name;


                        //miising item option on databse
                        foreach ($item->options()->get() as $io => $item_options) {
                            $option = [];

                            $item_options_obj = new StdClass();
                            $item_options_obj->id = $item_options->id;
                            $item_options_obj->item_id = $item_options->item_id;
                            $item_options_obj->item_group_id = $item_options->item_group_id;
                            $item_options_obj->option_id = $item_options->option_id;

                            foreach ($item->option()->get() as $o => $item_options) {

                                $option_obj = new StdClass();
                                $option_obj->id = $option->id;
                                $option_obj->name = $option->name;
                                $option_obj->price = $option->price;

                                $option[] = $option_obj;
                            }

                            $item_options_obj->option = $item_options->option;
                            $item_options[] = $item_options_obj;
                        }


                        $item_option_groups_obj->item_option_group = $item_option_groups;
                        $item_option_groups[] = $item_option_groups_obj;


            //$component_obj->item = $item_arr;
            //$components[] = $component_obj;

                    }


                    foreach ($item->itemTemplates()->get() as $it => $item_templates) {
                        $template = [];

                        $item_templates_obj = new StdClass();
                        $item_templates_obj->id = $item_templates->id;
                        $item_templates_obj->template_id = $item_templates->template_id;
                        $item_templates_obj->item_id = $item_templates->item_id;

                        foreach ($item->template()->get() as $t => $template) {
                            $template_obj = new StdClass();
                            $template_obj->id = $template->id;
                            $template_obj->name = $template->name;
                            $template_obj->row = $template->row;
                            $template_obj->col = $template->col;
                            $template_obj->thumbnail = $template->thumbnail;
                            $template_obj->margin = $template->margin;
                            $template_obj->padding = $template->padding;

                            $template[] = $template_obj;
                        }

                        $item_templates_obj->template = $template;
                        $item_templates[] = $item_templates_obj;
                    }

                    foreach ($item->itemBackgrounds()->get() as $ib => $item_backgrounds) {
                        $background_color = [];

                        $item_backgrounds_obj = new StdClass();
                        $item_backgrounds_obj->id = $item_backgrounds->id;
                        $item_backgrounds_obj->item_id = $item_backgrounds->item_id;
                        $item_backgrounds_obj->background_color_id = $item_backgrounds->background_color_id;


                        foreach ($item->background_color()->get() as $bc => $background_color) {
                            $background_color_obj = new StdClass();
                            $background_color_obj->id = $background_color->id;
                            $background_color_obj->color = $background_color->color;

                            $background_color[] = $background_color_obj;
                        }

                        $item_backgrounds_obj->background_color = $background_color;
                        $item_backgrounds[] = $item_backgrounds_obj;
                    }

                    foreach ($item->itemBackgroundImages()->get() as $ibi => $item_background_images) {
                        $background_image = [];

                        $item_background_images_obj = new StdClass();
                        $item_background_images_obj->id = $item_background_images->id;
                        $item_background_images_obj->item_id = $item_background_images->item_id;
                        $item_background_images_obj->background_image_id = $item_background_images->background_image_id;



                        foreach ($item->background_image()->get() as $bi => $background_image) {
                            $background_image_obj = new StdClass();
                            $background_image_obj->id = $background_image->id;
                            $background_image_obj->image = $background_image->image;

                            $background_image[] = $background_image_obj;
                        }

                        $item_background_images_obj->background_image = $background_image;
                        $item_background_images[] = $item_background_images_obj;


                    }

          $item_obj->item_option_groups = $item_option_groups;
          //$item_obj->item_option_groups = $options;


                    $item_obj->item_templates = $item_templates;
                    $item_obj->item_backgrounds = $item_backgrounds;
                    $item_obj->item_background_images = $item_background_images;

                    $item_arr[] = $item_obj;

                }

                $component_obj->item = $item_arr;

                $components[] = $component_obj;
            }

            $product_obj->product_components = $components;
            $data[] = $product_obj;
        }


        if(count($data) == 1){
            $data = $data[0];
        }

        return response()->json($data);
    }

}

这就是我从这个控制器获得的json任何人都可以解决这个问题吗?看起来像是嵌套出错了。请帮忙。

2 个答案:

答案 0 :(得分:0)

关于 json回复,您可以使用Laravel的内置JSON response

所以,不要自己构建json字段,让laravel为你做,就像这样:

return \Response::json( /* Array||Object to be converted */ );

//or

return response()->json( /* Array||Object to be converted */ );

关于可能未加载的嵌套关系。假设'products'和'items ......'是Models:

我认为一个好方法是使用模型中的'和''加载'方法,如下所示:

$products->load('components');

如果您需要多个嵌套关系,可以使用'点符号',如所描述的in this question

希望它有所帮助。

答案 1 :(得分:0)

Laravel响应函数自动将数组转换为JSON。

例如在我的控制器中,我有以下内容:

    $return_data = array('data' => 'some data','response' => 1);
    return response($return_data);

此外,json方法将自动将Content-Type标头设置为application / json,并使用json_encode PHP函数将给定数组转换为JSON:

return response()->json([
    'name' => 'Abigail',
    'state' => 'CA'
]);

有关详细信息,请访问:https://laravel.com/docs/5.1/responses#json-responses