【问题标题】:nested json response in laravel 5.7laravel 5.7 中的嵌套 json 响应
【发布时间】:2019-05-19 17:46:46
【问题描述】:

我是 laravel 的新手,我正在为三个表创建 json 响应,我正在尝试最多两个表,但我无法在编码中添加第三个表,请在我的函数中添加一些编码。表是

  1. category_type={category_type_id,category_name};
  2. ma​​in_category={main_category_id,category_type_id,main_category_name}
  3. sub_category={sub_category_id,category_type_id,main_category_id,sub_category_name}

我正在将 json 嵌套到第二个表,现在我想加入第三个表。

我创建的函数:

public function fetchCategory()
{
    $tableIds = DB::select( DB::raw("SELECT * FROM table_category_type"));

    $jsonResult = array();

    for($i = 0;$i < count($tableIds);$i++)
    {
        $jsonResult[$i]["category_id"] = $tableIds[$i]->category_id;
        $jsonResult[$i]["category_type"] = $tableIds[$i]->category_type;
        $id = $tableIds[$i]->category_id;
        $jsonResult[$i]["main_category"] = DB::select( DB::raw("SELECT *  FROM table_main_category WHERE category_type_id = $id"));

    }

    return Response::json(array(
                'success'     =>  '1',
                'data'    =>  $jsonResult),
                200
        );
}

Json 从我的代码中获得,它在第一个表中嵌套了第二个表,但我想在第一个表中嵌套第三个表。

    {
        "success": "1",
        "data": [
            {
                "category_id": 1,
                "category_type": "Study",
                "main_category": []
            },
            {
                "category_id": 2,
                "category_type": "Sports",
                "main_category": [
                    {
                        "main_category_id": 1,
                        "category_type_id": 2,
                        "category_name": "Popular Sports"
                    },
                    {
                        "main_category_id": 2,
                        "category_type_id": 2,
                        "category_name": "Team Sports"
                    },
                    {
                        "main_category_id": 3,
                        "category_type_id": 2,
                        "category_name": "Racquet Sports"
                    },
                    {
                        "main_category_id": 4,
                        "category_type_id": 2,
                        "category_name": "Fitness"
                    },
                    {
                        "main_category_id": 5,
                        "category_type_id": 2,
                        "category_name": "Recreation"
                    }
                ]
            },
            {
                "category_id": 3,
                "category_type": "Other",
                "main_category": []
            }
        ]
    }

我在 category_type 中嵌套 main_category 现在我想在 main_category 中嵌套 sub_category plz 需要更改编码。我在一对多和多对多关系中嵌套三个表。由于我是 laravel 的新手,我无法理解如何在嵌套关系中发送 id

【问题讨论】:

  • 您是否为这些表设置了 Eloquent 模型?
  • 我认为他在寻找自我参照关系stackoverflow.com/questions/20923773/…
  • 这根本不是你用 Laravel 做事的方式,看看雄辩的模型,这会变得容易得多。

标签: json laravel


【解决方案1】:

由于你是 Laravel 的新手,你可能不知道 Eloquent 是什么。 如果我理解你的问题,我们有 3 个表,我们需要通过关系将它们连接起来,这样我们就可以获取每个项目的关系对象和数据。

所以我们有 3 个表:

  1. category_types
  2. main_categories
  3. 子类别

注意:可以接受表格名称为复数形式,模型名称为单数形式。就像这个 category_types 用于表和 CategoryType 用于模型类。

好的,首先你需要创建迁移:

php artisan make:migration create_category_types_table

public function up()
{
    Schema::create('category_types', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name', 255)->nullable();
        $table->timestamps();
    });
}

php artisan make:migration create_main_categories_table

public function up()
{
    Schema::create('main_categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('category_type_id')->index();
        $table->string('name', 255)->nullable();
        $table->timestamps();
    });
}



php artisan make:migration create_sub_categories_table

public function up()
{
   Schema::create('sub_categories', function (Blueprint $table) {
       $table->bigIncrements('id');
       $table->bigInteger('category_type_id')->index();
       $table->bigInteger('main_category_id')->index();
       $table->string('name', 255)->nullable();
       $table->timestamps();
   });
}

所以在这次迁移的功能中,我们有一个自动增量和名称

请注意,您可以在此表中只写名称,而不是 category_name 和 category_type_id 你一直都知道这个属性是属于partuclar表的

好的,接下来我们可以创建和配置我们的模型。对于 CategoryTypes 模型,我们应该 为 main_categories 和 sub_categories 创建 2 个关系,如下所示:

php artisan make:model CategoryType
php artisan make:model MainCategory
php artisan make:model SubCategory

这是我们的 Category 类型模型代码:

class CategoryType extends Model {
    protected $fillable = ['name'];

    protected $dates = [
        'created_at',
        'updated_at'
    ];

    public function mainCategories()
    {
        return $this->hasMany(MainCategory::class);
    }

    public function subCategories() {
       return $this->hasMany(SubCategory::class);
    }
}

当我们定义我们的 CategoryType 模型时,我们现在可以在 Controller 中检索我们需要的数据:

public function fetchCategory()
{
    $categories = CategoryType::with(['mainCategories', 'subCategories'])->get();

    return response()->json($categories, 200);
}

还有我们的 json:

[
{
    "id": 1,
    "name": "Books",
    "created_at": "2019-05-19 13:24:51",
    "updated_at": "2019-05-19 13:24:51",
    "main_categories": [
        {
            "id": 1,
            "category_type_id": 1,
            "name": "Si-Fi",
            "created_at": "2019-05-19 13:26:07",
            "updated_at": "2019-05-19 13:26:08"
        },
        {
            "id": 2,
            "category_type_id": 1,
            "name": "Biography ",
            "created_at": "2019-05-19 13:26:33",
            "updated_at": "2019-05-19 13:26:34"
        },
        {
            "id": 3,
            "category_type_id": 1,
            "name": "Tall tale ",
            "created_at": "2019-05-19 13:26:57",
            "updated_at": "2019-05-19 13:26:58"
        },
        {
            "id": 4,
            "category_type_id": 1,
            "name": "Short story",
            "created_at": "2019-05-19 13:27:07",
            "updated_at": "2019-05-19 13:27:07"
        },
        {
            "id": 5,
            "category_type_id": 1,
            "name": "Fantasy",
            "created_at": "2019-05-19 13:27:17",
            "updated_at": "2019-05-19 13:27:18"
        }
    ],
    "sub_categories": [
        {
            "id": 1,
            "category_type_id": 1,
            "main_category_id": 1,
            "name": "Space exploration",
            "created_at": "2019-05-19 13:42:35",
            "updated_at": "2019-05-19 13:42:35"
        },
        {
            "id": 2,
            "category_type_id": 1,
            "main_category_id": 2,
            "name": "Historical biography",
            "created_at": "2019-05-19 13:42:36",
            "updated_at": "2019-05-19 13:42:36"
        },
        {
            "id": 3,
            "category_type_id": 1,
            "main_category_id": 5,
            "name": "The Lord of the Rings",
            "created_at": "2019-05-19 13:42:37",
            "updated_at": "2019-05-19 13:42:37"
        }
    ]
},
{
    "id": 2,
    "name": "Sports",
    "created_at": "2019-05-19 13:24:57",
    "updated_at": "2019-05-19 13:24:57",
    "main_categories": [
        {
            "id": 6,
            "category_type_id": 2,
            "name": "Popular Sports",
            "created_at": "2019-05-19 13:27:35",
            "updated_at": "2019-05-19 13:27:36"
        },
        {
            "id": 7,
            "category_type_id": 2,
            "name": "Team Sports",
            "created_at": "2019-05-19 13:27:35",
            "updated_at": "2019-05-19 13:27:36"
        },
        {
            "id": 8,
            "category_type_id": 2,
            "name": "Racquet Sports",
            "created_at": "2019-05-19 13:27:35",
            "updated_at": "2019-05-19 13:27:36"
        },
        {
            "id": 9,
            "category_type_id": 2,
            "name": "Fitness",
            "created_at": "2019-05-19 13:27:35",
            "updated_at": "2019-05-19 13:27:36"
        },
        {
            "id": 10,
            "category_type_id": 2,
            "name": "Recreation",
            "created_at": "2019-05-19 13:27:35",
            "updated_at": "2019-05-19 13:27:36"
        }
    ],
    "sub_categories": [
        {
            "id": 4,
            "category_type_id": 2,
            "main_category_id": 6,
            "name": "Football",
            "created_at": "2019-05-19 13:42:37",
            "updated_at": "2019-05-19 13:42:37"
        },
        {
            "id": 5,
            "category_type_id": 2,
            "main_category_id": 6,
            "name": "Basketball",
            "created_at": "2019-05-19 13:42:37",
            "updated_at": "2019-05-19 13:42:37"
        }
    ]
},
{
    "id": 3,
    "name": "Study",
    "created_at": "2019-05-19 13:25:24",
    "updated_at": "2019-05-19 13:25:25",
    "main_categories": [
        {
            "id": 11,
            "category_type_id": 3,
            "name": "Web Development",
            "created_at": "2019-05-19 13:27:35",
            "updated_at": "2019-05-19 13:27:36"
        },
        {
            "id": 12,
            "category_type_id": 3,
            "name": "Sofware Development",
            "created_at": "2019-05-19 13:27:35",
            "updated_at": "2019-05-19 13:27:36"
        },
        {
            "id": 13,
            "category_type_id": 3,
            "name": "Mangement",
            "created_at": "2019-05-19 13:27:35",
            "updated_at": "2019-05-19 13:27:36"
        },
        {
            "id": 14,
            "category_type_id": 3,
            "name": "Tourism",
            "created_at": "2019-05-19 13:27:35",
            "updated_at": "2019-05-19 13:27:36"
        },
        {
            "id": 16,
            "category_type_id": 3,
            "name": "Geography",
            "created_at": "2019-05-19 13:33:36",
            "updated_at": "2019-05-19 13:33:37"
        }
    ],
    "sub_categories": [
        {
            "id": 6,
            "category_type_id": 3,
            "main_category_id": 11,
            "name": "PHP",
            "created_at": "2019-05-19 13:42:29",
            "updated_at": "2019-05-19 13:42:30"
        },
        {
            "id": 7,
            "category_type_id": 3,
            "main_category_id": 11,
            "name": "Ruby",
            "created_at": "2019-05-19 13:42:31",
            "updated_at": "2019-05-19 13:42:32"
        },
        {
            "id": 8,
            "category_type_id": 3,
            "main_category_id": 11,
            "name": "Java",
            "created_at": "2019-05-19 13:42:33",
            "updated_at": "2019-05-19 13:42:33"
        }
    ]
}

]

希望这对您有所帮助。更多信息请阅读 Laravel 文档

【讨论】:

  • 您好先生,实际上我想要第一个表 id 名称为 'category_type_id' 我可以这样做吗?
  • 你的意思是 category_types 表ID?如果是,则不应有任何 category_type_id,因为 category_types 的此列可以是自动增量
  • 很好用,但我想在每个主要类别中嵌套 sub_category 数组,请帮助先生,您必须在 main_category 完成后在 json 中给出 sub_category 并且我想嵌套在 main_category 中
猜你喜欢
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 2017-05-26
  • 2020-02-07
  • 2020-12-31
  • 2018-08-02
  • 1970-01-01
  • 2020-10-23
相关资源
最近更新 更多