doucu7525 2018-11-29 04:35
浏览 43
已采纳

将MYSQL查询转换为Eloquent Laravel

this is my MYSQL query:

SELECT product_tasks.id AS product_task_id,
       tasks.id AS tasks_id,
       tasks.task_name AS label,
       tasks.serial_id AS serial_id,

  (SELECT serials.starting_series
   FROM serials
   WHERE serials.id = serial_id) AS starting_series
FROM product_tasks
JOIN tasks ON tasks.id = product_tasks.task_id
WHERE product_tasks.product_id = 1;

AND this is my eloquent query but I haven't finished with it:

ProductTask::select('product_tasks.id as product_task_id', 
                    'tasks.id as tasks_id', 'tasks.task_name as label', 
                    'tasks.serial_id as serial_id', * INSERT missing query here *)
            ->JOIN('tasks', 'tasks.id', 'product_tasks.task_id') 
            ->WHERE('product_tasks.product_id', 1) 
            ->get();

what is the missing part in my eloquent query?

  • 写回答

3条回答 默认 最新

  • dsfhe34889789708 2018-11-29 04:54
    关注

    Use DB:raw()

    ProductTask::select(
     'product_tasks.id as product_task_id',
     'tasks.id as tasks_id',
     'tasks.task_name as label',
     'tasks.serial_id as serial_id',
     DB::raw("SELECT serials.starting_series as starting_series FROM serials WHERE serials.id = serial_id")) 
     ->join('tasks', 'tasks.id', 'product_tasks.task_id')
     ->where('product_tasks.product_id', 1)
     ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档