dongqu5650 2017-04-24 11:03
浏览 118
已采纳

如何在由自定义分隔符分隔的单个列中一起显示两个组concat列

The follow is the controller function to display the table

$rec = DB::table('recipe_list')
    ->select('recipe_list.recipe_id', 'recipe_list.Recipe_name',
        'recipe_list.Recipe_desc', 'recipe_list.Recipe_duration',
        DB::raw('group_concat(distinct ing_list.Ing_name separator ",") as recipe_ingredients'),
        DB::raw('group_concat(distinct recipe_inglist.quantity  separator ",") as ingredients_amount'),
        'recipe_cusine.Cusine_name', 'recipe_type.Recipe_type_name', 'recipe_list.image',
        DB::raw(' count(distinct likerecipes.id) as likes'))
    ->join('recipe_inglist', 'recipe_list.recipe_id', '=', 'recipe_inglist.Recipe_id')
    ->join('ing_list', 'recipe_inglist.Ing_id', '=', 'ing_list.ing_id')
    ->join('recipe_cusine', 'recipe_list.Recipe_cusine_id', '=', 'recipe_cusine.cusine_id')
    ->join('recipe_type', 'recipe_list.Recipe_type_id', '=', 'recipe_type.Recipe_typeID')
    ->join('likerecipes', 'recipe_list.recipe_id', '=', 'likerecipes.recipe_id')
    ->where('recipe_list.recipe_id', '>=', '1')
    ->groupBy('recipe_list.recipe_id', 'recipe_list.recipe_name', 'recipe_list.recipe_desc', 'recipe_list.recipe_duration', 'recipe_cusine.Cusine_name', 'recipe_type.Recipe_type_name', 'recipe_list.image')->get();

the table output is Recipes Table

But what I want the output to be is like :

|Recipe_id|Name|Description|Cusine|Type |Ingredient        |
|   1     |blah|blahblahbla| bla  | bla | 20 gm of garlic,
                                        | 500 gm of chicken,
                                        | 30 gm of onion,

This is my recipe_inglist table recipe_inglist

  • 写回答

1条回答 默认 最新

  • doulian5857 2017-04-24 11:14
    关注

    I recommend you to go with php code,

    Like this way,

    You have $rec data, then do below code,

    foreach ($rec as $k => &$v) {
        $names  = explode(",", $v->recipe_ingredients);
        $amount = explode(",", $v->ingredients_amount);
        $temp   = [];
        foreach ($names as $key => $value) {
            $temp[] = $amount[$key] . ' of ' . $value;
        }
        if ($temp) {
            $v->ingredient = implode(",", $temp);
        }
    
    }
    

    Where I am creating ingradient field which will have e.g. 20 gm of garlic,500 gm of chicken,30 gm of onion as value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置