如何从 2 个表创建嵌套的 json 对象查询?

我需要做出一个json看起来像这样的结果


{"data":[

    {"id":2,

    "name":"miles",

    "email":"sb-o8c47x137031@personal.example.com",

    "details":{

        "id":11,

        "product_id":"product-003","purchase_rebate":2,

        "purchase_rebate_date":"2019-09-10 23:07:33",

        "survey_ans_rebate":2,"survey_ans_rebate_date":"2019-09-10 23:07:33",

        "created_at":"2019-09-10 23:07:33","updated_at":"2019-09-10 23:07:33"

        },

    },

    {"id":3,

    "name":"Jayson",

    "email":"sb-rv9me150874@personal.example.com",

    "details":{

        "id":13,

        "product_id":"product-010",

        "purchase_rebate":2,

        "purchase_rebate_date":"2019-09-11 04:26:24",

        "survey_ans_rebate":2,

        "survey_ans_rebate_date":"2019-09-11 04:26:24",

        "created_at":"2019-09-11 04:26:24",

        "updated_at":"2019-09-11 04:26:24"}

    }

    ]

}

这是我的服务器端 PHP 脚本


$get_merchant_users = DB::table('merchant_users as a')

            ->leftjoin('user_transactions as b', 'b.email','=','a.email') 

            ->select('a.id','a.name',DB::raw('SUM(b.purchase_rebate) + SUM(b.survey_ans_rebate) as total'),'a.email')

            ->groupBy('a.id','a.name','a.email')         

            ->get();

            $details = DB::table('user_transactions')

            ->select('*')

            ->get();

            $data = array('data'=> $get_merchant_users,'details'=> $details);

            return $data;


jeck猫
浏览 133回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP