使用Laravel列中的连接和逗号分隔值执行查询 [英] Perform query with join and comma separated values in column in Laravel

查看:116
本文介绍了使用Laravel列中的连接和逗号分隔值执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有列文件的表 orders .该列包含文件ID,并且用逗号分隔.

I have table orders which has column files. The column holds file id's and they are comma separated.

第二张表是 documents .

表名称:订单

 orders_id | order_details | file_id
 ------------------------------------
     1     | some details  |   1,2    

表名:文档

 id |   name 
 ------------------
  1 | file name
  2 | file2 name2

目前,我有此查询,该查询仅查询 orders 表,但我也想加入文档,因此我可以向客户显示在 file_id 列中连接的文档的名称.按顺序

Currently I have this query which query only orders table but I want to join documents also so I can show to customer the names of documents which are connected in file_id column in orders

$docomuntOrders = Order::where('user_id',getCurrentUser()->user_id)
                        ->orderBy('order_id', 'DESC')
                        ->paginate(10);

您能在这里指导一下吗?

Can you guide a little bit here?

推荐答案

希望它会为您工作.试试看

Hope It will work for you. have a try

$docomuntOrders = \DB::table("orders")
            ->select("orders.*",\DB::raw("GROUP_CONCAT(documents.name) as docname"))
            ->leftjoin("documents",\DB::raw("FIND_IN_SET(documents.id,orders.file_id)"),">",\DB::raw("'0'"))
            ->where('user_id',getCurrentUser()->user_id)
            ->groupBy("orders.id")
            ->paginate(10);

如果尝试使用 dd($ docomuntOrders),希望它会返回所需的结果.

if you try it dd($docomuntOrders) hope it will return desired result.

这篇关于使用Laravel列中的连接和逗号分隔值执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆