来自PHP中许多表的嵌套json响应

时间:2020-02-05 06:57:56

标签: php mysql json laravel

到目前为止,我可以使用laravel调用API。

我的数据库中有2个不同的views

我有view_project_percentageview_projtask

view_project_percentage是包含项目详细信息的项目列表。

view_projtask是每个项目的任务列表

例如。

如果我的3 projects中有view_project_percentage

喜欢

ProjCode of ABC1,ABC2, ABC3

并在我的view_projtask

我有

2 task for ProjectCode ABC1

1 task for ProjectCode ABC2

3 task for ProjectCode ABC3

现在我想做的就是得到这样的json数组响应

 [
{
    "id": 9,
    "proj_code": "ZQQKVOTRJJNZ",
    "proj_title": "P12",
    "proj_desc": "Project Description 12",
    "target_man_days": 1000000,
    "issue": 0,
    "report": 1,
    "total_employee": 3,
    "hours_8": "4",
    "hours_6": "0.0",
    "hours_3": "0",
    "total_weight_progress": "5.00",
    "est_start_date": "2020-01-20 09:42:00",
    "est_end_date": "2020-01-20 21:42:00",
    "act_start_date": "2020-01-20 09:42:00",
    "act_end_date": "2020-01-20 21:42:00",
    "longitude": "121.07642460",
    "latitude": "14.56055010",
    "location": "Pasig, Metro Manila, Philippinae",
    "status": null,
    "deleted": 0,
    "by_id": 7,
    "updated_by": "Keigh Dee",
    "created_at": "2020-01-20 09:42:31",
    "updated_at": "2020-01-22 14:01:08",
    {
       "taskCode": "J5X1FHCMVFSQ",
       "total_task_weight": null,
       "taskWeight": "50.00",
       "plan_days": "5",
       "actual_days": "5",
       "task_title": "Task 5",
       "task_desc": "Description 5",
       "taskDeleted": 0,
        "view_projtask_deleted": 0
    },
    {
        "taskCode": "FZPRFIWOKBFQ",
        "total_task_weight": "5.00",
        "taskWeight": "25.00",
        "plan_days": "5",
        "actual_days": "5",
        "task_title": "Task 2",
        "task_desc": "Description 2",
        "taskDeleted": 0,
        "view_projtask_deleted": 0
   }
},

........so on

正如我在示例json响应中看到的那样

1 project with 2 tasks

这是我的工作代码

public function get_all_projtask()
{
    $proj_query = "SELECT a.id, a.proj_code, a.proj_title, a.proj_desc, a.target_man_days, a.issue, a.report, a.total_employee, a.hours_8, a.hours_6, a.hours_3,
    a.total_weight_progress, a.est_start_date, a.est_end_date, a.act_start_date, a.act_end_date, a.longitude, a.latitude, a.location, a.status, a.deleted, 
    a.by_id, a.updated_by, a.created_at, a.updated_at, b.taskCode, b.total_task_weight, b.taskWeight, b.plan_days, b.actual_days, b.task_title, b.task_desc, b.taskDeleted, b.deleted as view_projtask_deleted

    FROM `view_projtask` AS b LEFT JOIN `view_project_percentage` AS a ON b.projCode = a.proj_code";

    $proj = DB::connection('mysql')->select($proj_query);

    if(count($proj)){
        return $proj;
    }else{
        return response([
            'status'=>'bad',
            'message'=>'No record found'
            ]);
    }
}

我在这里的查询只是从view_projtask

获取每个特定项目的所有任务

我正在尝试执行一个json响应,以唯一方式获取所有项目列表,并且在该项目的json内部包含多个任务

我的view_projtask

enter image description here

我的view_project_percentage

enter image description here

更新

这是我目前从api发出的输出

[
{
    "id": 9,
    "proj_code": "ZQQKVOTRJJNZ",
    "proj_title": "P12",
    "proj_desc": "Project Description 12",
    "target_man_days": 1000000,
    "issue": 0,
    "report": 1,
    "total_employee": 3,
    "hours_8": "4",
    "hours_6": "1.0",
    "hours_3": "0",
    "total_weight_progress": "5.00",
    "est_start_date": "2020-01-20 09:42:00",
    "est_end_date": "2020-01-20 21:42:00",
    "act_start_date": "2020-01-20 09:42:00",
    "act_end_date": "2020-01-20 21:42:00",
    "longitude": "121.07642460",
    "latitude": "14.56055010",
    "location": "Pasig, Metro Manila, Philippinae",
    "status": null,
    "deleted": 0,
    "by_id": 7,
    "updated_by": "Keigh Dee",
    "created_at": "2020-01-20 09:42:31",
    "updated_at": "2020-01-22 14:01:08",
    "taskCode": "OKIX19IR1DST",
    "total_task_weight": null,
    "taskWeight": "25.00",
    "plan_days": "5",
    "actual_days": "5",
    "task_title": "Task 1",
    "task_desc": "Description 1",
    "taskDeleted": 0,
    "view_projtask_deleted": 0
},
{
    "id": 9,
    "proj_code": "ZQQKVOTRJJNZ",
    "proj_title": "P12",
    "proj_desc": "Project Description 12",
    "target_man_days": 1000000,
    "issue": 0,
    "report": 1,
    "total_employee": 3,
    "hours_8": "4",
    "hours_6": "1.0",
    "hours_3": "0",
    "total_weight_progress": "5.00",
    "est_start_date": "2020-01-20 09:42:00",
    "est_end_date": "2020-01-20 21:42:00",
    "act_start_date": "2020-01-20 09:42:00",
    "act_end_date": "2020-01-20 21:42:00",
    "longitude": "121.07642460",
    "latitude": "14.56055010",
    "location": "Pasig, Metro Manila, Philippinae",
    "status": null,
    "deleted": 0,
    "by_id": 7,
    "updated_by": "Keigh Dee",
    "created_at": "2020-01-20 09:42:31",
    "updated_at": "2020-01-22 14:01:08",
    "taskCode": "FZPRFIWOKBFQ",
    "total_task_weight": "5.00",
    "taskWeight": "25.00",
    "plan_days": "5",
    "actual_days": "5",
    "task_title": "Task 2",
    "task_desc": "Description 2",
    "taskDeleted": 0,
    "view_projtask_deleted": 0
},
{
    "id": 9,
    "proj_code": "ZQQKVOTRJJNZ",
    "proj_title": "P12",
    "proj_desc": "Project Description 12",
    "target_man_days": 1000000,
    "issue": 0,
    "report": 1,
    "total_employee": 3,
    "hours_8": "4",
    "hours_6": "1.0",
    "hours_3": "0",
    "total_weight_progress": "5.00",
    "est_start_date": "2020-01-20 09:42:00",
    "est_end_date": "2020-01-20 21:42:00",
    "act_start_date": "2020-01-20 09:42:00",
    "act_end_date": "2020-01-20 21:42:00",
    "longitude": "121.07642460",
    "latitude": "14.56055010",
    "location": "Pasig, Metro Manila, Philippinae",
    "status": null,
    "deleted": 0,
    "by_id": 7,
    "updated_by": "Keigh Dee",
    "created_at": "2020-01-20 09:42:31",
    "updated_at": "2020-01-22 14:01:08",
    "taskCode": "J5X1FHCMVFSQ",
    "total_task_weight": null,
    "taskWeight": "50.00",
    "plan_days": "5",
    "actual_days": "5",
    "task_title": "Task 5",
    "task_desc": "Description 5",
    "taskDeleted": 0,
    "view_projtask_deleted": 0
},
{
    "id": 8,
    "proj_code": "SUZ82OJI091M",
    "proj_title": "P1",
    "proj_desc": "Project Description 1",
    "target_man_days": 10000,
    "issue": 0,
    "report": 0,
    "total_employee": 2,
    "hours_8": "1",
    "hours_6": "0.5",
    "hours_3": "0",
    "total_weight_progress": null,
    "est_start_date": "2020-01-20 09:41:00",
    "est_end_date": "2020-01-20 21:41:00",
    "act_start_date": "2020-01-20 09:41:00",
    "act_end_date": "2020-01-20 21:41:00",
    "longitude": "121.07642460",
    "latitude": "14.56055010",
    "location": "Pasig, Metro Manila, Philippinae",
    "status": null,
    "deleted": 0,
    "by_id": 7,
    "updated_by": "Keigh Dee",
    "created_at": "2020-01-20 09:41:29",
    "updated_at": "2020-01-20 09:41:29",
    "taskCode": "J5X1FHCMVFSQ",
    "total_task_weight": null,
    "taskWeight": "50.00",
    "plan_days": "5",
    "actual_days": "5",
    "task_title": "Task 5",
    "task_desc": "Description 5",
    "taskDeleted": 0,
    "view_projtask_deleted": 0
},

1 个答案:

答案 0 :(得分:2)

如果您考虑使用雄辩的(您确实应该使用),则这些表的模型应类似于:

class Project extends Model
{
    protected $table = "view_project_percentage";

    public function tasks()
    {
        return $this->hasMany(ProjectTask::class, 'projCode', 'proj_code');
    }
}

class ProjectTask extends Model
{
    protected $table = "view_projtask";

    public function project()
    {
        return $this->belongsTo(Project::class, 'projCode', 'proj_code');
    } 
}

然后您需要在控制器中完成所有操作:

public function get_all_projtask()
{   
    return response()->json(Project::with('tasks')->get());
}