【问题标题】:JSON formatting in LaravelLaravel 中的 JSON 格式
【发布时间】:2018-03-17 04:23:58
【问题描述】:

假设我有三个模型和表格,即学校、班级和学生。 学校和班级是一对多的关系,班级和学生也是一对多的关系。现在我正在创建一个 RESTFUL API 以这种 JSON 格式一次发送所有这些数据:

[{
        "1": {
            "school_id": 1,
            "school_name": "Havard",
            "Class": {
                "1": {
                    "class_id": 1,
                    "school_id": 1,
                    "class": "6",
                    "Student": {
                        "1": {
                            "Student_fname": "SomeName",
                            "Student_lname": "Last Name"
                        },
                        "2": {
                            "Student_fname": "Another",
                            "Student_lname": "AnotherLast"
                        }
                    },
                    "2": {
                        "class_id": 2,
                        "school_id": 28,
                        "class": "7",
                        "Student": {
                            "1": {
                                "Student_fname": "Anotherone",
                                "Student_lname": "Last"
                            },
                            "2": {
                                "Student_fname": "New",
                                "Student_lname": "Newer"
                            }
                        }
                    }
                }
            }
        }

    },
    {
        "2": {
            "school_id": 1,
            "school_name": "AnotherSchool",
            "Class": {
                "1": {
                    "class_id": 1,
                    "school_id": 1,
                    "class": "6",
                    "Student": {
                        "1": {
                            "Student_fname": "SomeName",
                            "Student_lname": "Last Name"
                        },
                        "2": {
                            "Student_fname": "Another",
                            "Student_lname": "AnotherLast"
                        }
                    },
                    "2": {
                        "class_id": 2,
                        "school_id": 28,
                        "class": "7",
                        "Student": {
                            "1": {
                                "Student_fname": "Anotherone",
                                "Student_lname": "Last"
                            },
                            "2": {
                                "Student_fname": "New",
                                "Student_lname": "Newer"
                            }
                        }
                    }
                }
            }
        }
    }
]

如何实现这种格式?我的这个 json 格式正确吗?有没有更好的方法来实现它?

【问题讨论】:

  • 我认为类(如果类不止一个)和学生应该是对象数组(json)。
  • 是的,一所学校的课程不止一个。你能举一个代码的例子来说明清楚吗?

标签: json laravel rest api


【解决方案1】:

【讨论】:

    【解决方案2】:

    您可以使用 https://github.com/thephpleague/fractal、https://github.com/dingo/api 之类的库,或者通过在控制器(或您喜欢的类)中创建转换器方法来实现

    一些伪代码

    <?php
    class UserController extends Controller
    {
    
        public function index(Request $request) {
            $users = Users::all(); 
    
            $response = []; 
    
            foreach ($users as $user) {
                $response[] = $this->transform($user); 
            }
    
        }
    
        public function transform(User $user) {
            return [
                'username' => $user->username,
                'age' => $user->age,
                'class' => [
                    'name' => $user->school->name,
                    'something' => $user->school->something
                ]
            ];
        }
    
    }
    

    我强烈建议在控制器本身之外的其他类中执行转换部分。

    【讨论】:

      【解决方案3】:

      我觉得应该是这样的

      [
      "1": {
      "school_id": 1,
          "school_name": "Havard",
          "Class": [
          "1": {
                  "class_id": 1,
                  "school_id": 1,
                  "class": "6",
                  "Student": [
                      "1": {
                      "Student_fname": "SomeName",
                          "Student_lname": "Last Name"
                      }
                  ]
               }
          ]
      }
      ]
      

      【讨论】:

        猜你喜欢
        • 2018-08-26
        • 1970-01-01
        • 2014-01-07
        • 2015-04-15
        • 2017-08-29
        • 1970-01-01
        • 2020-03-30
        • 1970-01-01
        • 2019-05-12
        相关资源
        最近更新 更多