donglu7998 2018-10-31 08:59
浏览 155

将原始SQL查询转换为Laravel Eloquent

My project is a content management system & a new version of it will be integrated in a Laravel framework; and there are lots of huge SQL queries which needs to be converted into eloquent syntax.

I found the following tool to convert my already written queries into eloquent syntax, but it does not work.

Could anyone guide me to convert my queries or help me find a tool that could do the job?

Please find below a sample of my queries (note that most of my queries look similar):

SELECT id, name, code,
       ( SELECT price FROM productprice WHERE id = p.id ) AS price,
       ( SELECT image FROM productpictures WHERE id = p.id LIMIT 1 ) 
AS image
FROM product p 
WHERE categoryid = 1

Thank you.

  • 写回答

6条回答 默认 最新

  • dongxing1965 2018-10-31 09:09
    关注

    This is the best tool in my opinion :) reading and knowing the documentation, because no tool will be able to do it automagically for every case.

    So here is my attempt on your query above:

    DB::table('product as p')
       ->select([
           'id',
           'name',
           'code',
           'pp.price',
           'ppic.image'
       ])
       ->join('productprice as pp', 'pp.id', '=', 'p.id')
       ->join('productpictures as ppic', 'ppic.id', '=', 'p.id')
       ->where('categoryid', 1)
       ->get();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题