如何使用json laravel的单个键获得多个值?

jchrr9hc  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(335)

嗨,我想问一下如何在数组中发送json,而不是逐个发送。我使用的是laravel和vue.js我在laravel是新的,我想知道更好,我试图循环,但它失败了
举个例子:

{
    "data": [
        [
            {
                "activity_id": 1,
                "start_date": "2018-11-06",
                "product_name": "ks",
                "topic_name": "exploration"
            }
        ],
        [
            {
                "subproduct_name": "bcd"
            },
            {
                "subproduct_name": "cfr"
            }
        ]
    ]
}

我想把它们弄成这样

{
    "data": [
        [
            {
                "activity_id": 1,
                "start_date": "2018-11-06",
                "product_name": "ks",
                "topic_name": "exploration",
                "subproduct_name":["bcd","cfr"]
            }
        ],

    ]
}

这是我的控制器

public function getActivity()
{

    $getData = DB::table('new__businesses')->select('new__businesses.activity_id','new__businesses.start_date','new__businesses.end_date','new__businesses.corporate_name','ms__subsidiaries.subsidiary_name','ms__products.product_name','ms__topics.topic_name')
    ->leftjoin('ms__subsidiaries','ms__subsidiaries.subsidiary_id','=','new__businesses.subsidiary_id')
    ->join('ms__products','ms__products.product_id','=','new__businesses.product_id')
    ->join('ms__topics','ms__topics.topic_id','=','new__businesses.topic_id')
    ->get();

    $getSubproduct = DB::table('new__businesses')
        ->select('ms__subproducts.subproduct_name')
        ->join('store__subproducts','store__subproducts.activity_id','=','new__businesses.activity_id')
        ->join('ms__subproducts','ms__subproducts.subproduct_id','=','store__subproducts.subproduct_id')
        ->get();

    $getPic = DB::table('new__businesses')
        ->select('ms__users.user_name')
        ->join('pics','pics.activity_id','=','new__businesses.activity_id')
        ->join('ms__users','ms__users.user_id','=','pics.user_id')
        ->get();

        return response()->json([
            'data'=>$getData,
            'pic'=>$getPic,
            'subproduct'=>$getSubproduct
        ]);        

}

}
请给我解决办法,我很累

s3fp2yjn

s3fp2yjn1#

如果你用拔毛代替 get ,您将获得单个值数组,而不是对象数组,然后可以将其添加到数组中。

$getSubproduct = DB::table('new__businesses')
    ->join('store__subproducts','store__subproducts.activity_id','=','new__businesses.activity_id')
    ->join('ms__subproducts','ms__subproducts.subproduct_id','=','store__subproducts.subproduct_id')
    ->pluck('ms__subproducts.subproduct_name');

相关问题