我目前正在执行一个原始的SQL查询,但这会导致关系和模型引导方法出现问题。
是否可以使用Laravel Eloquent模型通过关系来执行以下SQL查询?请注意,所有的数据库表都有定义的外键,并且关系要么是HasOne关系,要么是HasMany关系。
$timeBreakDown = DB::select(
"SELECT
Entries.`task_id`,
Entries.`opportunity_id`,
SUM(Entries.`total_duration`) as 'duration',
Class.`class` as 'class',
Subclass.`sub_class` as 'subclass'
from entries Entries
INNER JOIN `tasks` Task
ON task_id = Task.id
INNER JOIN `task_class` Class
ON Task.`class_id` = Class.`id`
INNER JOIN `task_subclasses` Subclass
ON Task.`subclass_id` = Subclass.`id`
WHERE Entries.`opportunity_id` = '".$opportunity->id."'
GROUP BY Entries.`task_id`"
);
模型是什么?
Entries
Tasks
Class
Subclass
如何构建模型关系以处理上述SQL查询?