如何将 SQL 查询转换为 laravel 查询?

How to convert SQL query in laravel query?

我知道这个问题问得不是很好,所以很抱歉,我SQL查询了这个

`SELECT
    c.*
FROM
    merchantlink m,
    company c,
    merchantlinkrelation mlr
WHERE
    (m.initiator_user_id = c.owner_user_id AND
     m.responder_user_id = 86 AND
     mlr.ptype='dealer')
     OR
    (m.initiator_user_id = 86 AND
     m.responder_user_id = c.owner_user_id AND
     mlr.ptype = 'dealer')
     OR
    (m.initiator_user_id = c.owner_user_id AND
     c.owner_user_id=86 AND
     mlr.ptype='dealer')
GROUP BY
    c.id;`

我想在 PHP laravel 查询表单中转换它所以尝试了这个查询

 $twowaycompany = DB::table('company')
                ->join('merchantlink','merchantlink.responder_user_id', 'company.owner_user_id')
->join('merchantlinkrelation','merchantlinkrelation.merchantlink_id','merchantlink.id')
                
                ->orWhere('merchantlink.initiator_user_id', 86)
                ->join('merchantlink','merchantlink.initiator_user_id', 'company.owner_user_id')
                ->orWhere('merchantlink.responder_user_id', 86)
                ->pluck('name')->toArray();

但我不知道 SQL 甚至我也不明白我是如何转换它的,有人可以帮助将 SQL 查询转换为 laravel 查询吗?

这里是 db 图片商家 link

商家link关系

拜托,我已尝试创建一个查询(Laravel 查询生成器)。请检查并告诉我它是否有效。

DB::table(DB::raw("merchantlink m, company c, merchantlinkrelation mlr"))
->select("c.*")
->whereRaw ("(m.initiator_user_id = c.owner_user_id and m.responder_user_id = 86 and mlr.ptype = 'dealer')")
->orWhereRaw("(m.initiator_user_id = 86 and m.responder_user_id = c.owner_user_id and mlr.ptype = 'dealer')")
->orWhereRaw("(m.initiator_user_id = c.owner_user_id and c.owner_user_id = 86 and mlr.ptype = 'dealer')")
->groupBy("c.id")
->get();

你可以简单地这样做:

DB::Select(
  DB::Raw('
    SELECT
      c.*
    FROM
      merchantlink m,
      company c,
      merchantlinkrelation mlr
    WHERE
       (m.initiator_user_id = c.owner_user_id AND
       m.responder_user_id = 86 AND
       mlr.ptype='dealer')
    OR
       (m.initiator_user_id = 86 AND
       m.responder_user_id = c.owner_user_id AND
       mlr.ptype = 'dealer')
    OR
       (m.initiator_user_id = c.owner_user_id AND
       c.owner_user_id=86 AND
       mlr.ptype='dealer')
    GROUP BY
       c.id
  ')
);

但可能有更好的方法。